Adrefill Visibility (Settings API). * * @package ApevPostElementVisibility */ defined( 'ABSPATH' ) || exit; add_action( 'admin_menu', 'apev_add_settings_page' ); add_action( 'admin_init', 'apev_register_settings' ); add_filter( 'plugin_action_links_' . plugin_basename( APEV_PLUGIN_FILE ), 'apev_plugin_action_links' ); /** * Add the settings page under Settings. */ function apev_add_settings_page() { add_options_page( __( 'Adrefill Visibility', 'adrefill-post-element-visibility' ), __( 'Adrefill Visibility', 'adrefill-post-element-visibility' ), 'manage_options', 'adrefill-post-element-visibility', 'apev_render_settings_page' ); } /** * Register the option, sections and fields. */ function apev_register_settings() { register_setting( 'apev_settings_group', 'apev_settings', array( 'type' => 'array', 'sanitize_callback' => 'apev_sanitize_settings', 'default' => apev_default_settings(), 'show_in_rest' => false, ) ); add_settings_section( 'apev_section_defaults', __( 'Default visibility', 'adrefill-post-element-visibility' ), 'apev_render_section_defaults', 'adrefill-post-element-visibility' ); add_settings_section( 'apev_section_behavior', __( 'Behavior', 'adrefill-post-element-visibility' ), 'apev_render_section_behavior', 'adrefill-post-element-visibility' ); add_settings_field( 'apev_field_scope', __( 'Where to hide', 'adrefill-post-element-visibility' ), 'apev_render_field_scope', 'adrefill-post-element-visibility', 'apev_section_behavior' ); add_settings_field( 'apev_field_compat', __( 'Theme compatibility', 'adrefill-post-element-visibility' ), 'apev_render_field_compat', 'adrefill-post-element-visibility', 'apev_section_behavior' ); } /** * Sanitize the whole settings array. * * @param mixed $input Raw input. * @return array */ function apev_sanitize_settings( $input ) { $clean = apev_default_settings(); if ( ! is_array( $input ) ) { return $clean; } $clean['scope'] = ( isset( $input['scope'] ) && 'everywhere' === $input['scope'] ) ? 'everywhere' : 'singular'; $clean['css_fallback'] = empty( $input['css_fallback'] ) ? 0 : 1; $clean['block_meta_cleanup'] = empty( $input['block_meta_cleanup'] ) ? 0 : 1; $global = ( isset( $input['global'] ) && is_array( $input['global'] ) ) ? $input['global'] : array(); foreach ( apev_get_element_keys() as $element ) { $clean['global'][ $element ] = empty( $global[ $element ] ) ? 0 : 1; } $types = ( isset( $input['post_types'] ) && is_array( $input['post_types'] ) ) ? $input['post_types'] : array(); foreach ( apev_get_supported_post_types() as $post_type ) { if ( ! isset( $types[ $post_type ] ) || ! is_array( $types[ $post_type ] ) ) { continue; } foreach ( apev_get_element_keys() as $element ) { $value = isset( $types[ $post_type ][ $element ] ) ? apev_sanitize_toggle( $types[ $post_type ][ $element ] ) : ''; if ( '' !== $value ) { $clean['post_types'][ $post_type ][ $element ] = $value; } } } return $clean; } /** * Render the settings page. */ function apev_render_settings_page() { if ( ! current_user_can( 'manage_options' ) ) { return; } ?>

' . esc_html__( 'Choose what is hidden when a post or page has no setting of its own. Each item can still override this in the editor.', 'adrefill-post-element-visibility' ) . '

'; ?> $label ) : ?> $label ) : ?>
/>
labels->name ); ?>
' . esc_html__( 'Fine-tune where and how hidden elements are removed.', 'adrefill-post-element-visibility' ) . '

'; } /** * Scope field. */ function apev_render_field_scope() { $settings = apev_get_settings(); ?>


%2$s', esc_url( $url ), esc_html__( 'Settings', 'adrefill-post-element-visibility' ) ) ); return $links; }