diff --git a/plugins/view-transitions/includes/settings.php b/plugins/view-transitions/includes/settings.php index e7ce54a514..bf77e7cde0 100644 --- a/plugins/view-transitions/includes/settings.php +++ b/plugins/view-transitions/includes/settings.php @@ -45,9 +45,11 @@ function plvt_get_view_transition_animation_labels(): array { * @since 1.0.0 * @see plvt_sanitize_view_transitions_theme_support() * - * @return array{ default_transition_animation: non-empty-string, header_selector: non-empty-string, main_selector: non-empty-string, post_title_selector: non-empty-string, post_thumbnail_selector: non-empty-string, post_content_selector: non-empty-string, enable_admin_transitions: bool } { + * @return array{ override_theme_config: bool, default_transition_animation: non-empty-string, header_selector: non-empty-string, main_selector: non-empty-string, post_title_selector: non-empty-string, post_thumbnail_selector: non-empty-string, post_content_selector: non-empty-string, enable_admin_transitions: bool } { * Default setting value. * + * @type bool $override_theme_config Whether to override the current theme's configuration. Otherwise, + * the other frontend specific settings won't be applied. * @type string $default_transition_animation Default view transition animation. * @type string $header_selector CSS selector for the global header element. * @type string $main_selector CSS selector for the global main element. @@ -59,6 +61,7 @@ function plvt_get_view_transition_animation_labels(): array { */ function plvt_get_setting_default(): array { return array( + 'override_theme_config' => false, 'default_transition_animation' => 'fade', 'header_selector' => 'header', 'main_selector' => 'main', @@ -74,9 +77,11 @@ function plvt_get_setting_default(): array { * * @since 1.0.0 * - * @return array{ default_transition_animation: non-empty-string, header_selector: non-empty-string, main_selector: non-empty-string, post_title_selector: non-empty-string, post_thumbnail_selector: non-empty-string, post_content_selector: non-empty-string, enable_admin_transitions: bool } { + * @return array{ override_theme_config: bool, default_transition_animation: non-empty-string, header_selector: non-empty-string, main_selector: non-empty-string, post_title_selector: non-empty-string, post_thumbnail_selector: non-empty-string, post_content_selector: non-empty-string, enable_admin_transitions: bool } { * Stored setting value. * + * @type bool $override_theme_config Whether to override the current theme's configuration. Otherwise, + * the other frontend specific settings won't be applied. * @type string $default_transition_animation Default view transition animation. * @type string $header_selector CSS selector for the global header element. * @type string $main_selector CSS selector for the global main element. @@ -96,9 +101,11 @@ function plvt_get_stored_setting_value(): array { * @since 1.0.0 * * @param mixed $input Setting to sanitize. - * @return array{ default_transition_animation: non-empty-string, header_selector: non-empty-string, main_selector: non-empty-string, post_title_selector: non-empty-string, post_thumbnail_selector: non-empty-string, post_content_selector: non-empty-string, enable_admin_transitions: bool } { + * @return array{ override_theme_config: bool, default_transition_animation: non-empty-string, header_selector: non-empty-string, main_selector: non-empty-string, post_title_selector: non-empty-string, post_thumbnail_selector: non-empty-string, post_content_selector: non-empty-string, enable_admin_transitions: bool } { * Sanitized setting. * + * @type bool $override_theme_config Whether to override the current theme's configuration. Otherwise, + * the other frontend specific settings won't be applied. * @type string $default_transition_animation Default view transition animation. * @type string $header_selector CSS selector for the global header element. * @type string $main_selector CSS selector for the global main element. @@ -140,9 +147,14 @@ function plvt_sanitize_setting( $input ): array { } } - // Sanitize "enable_admin_transitions" as a boolean. - if ( isset( $input['enable_admin_transitions'] ) ) { - $value['enable_admin_transitions'] = (bool) $input['enable_admin_transitions']; + $checkbox_options = array( + 'override_theme_config', + 'enable_admin_transitions', + ); + foreach ( $checkbox_options as $checkbox_option ) { + if ( isset( $input[ $checkbox_option ] ) ) { + $value[ $checkbox_option ] = (bool) $input[ $checkbox_option ]; + } } return $value; @@ -188,20 +200,26 @@ function plvt_register_setting(): void { * @since 1.0.0 * @access private * - * @global array $_wp_theme_features Theme support features added and their arguments. + * @global bool|null $plvt_has_theme_support_with_args Whether the current theme explicitly supports view transitions with custom config. + * @global array $_wp_theme_features Theme support features added and their arguments. */ function plvt_apply_settings_to_theme_support(): void { - global $_wp_theme_features; + global $plvt_has_theme_support_with_args, $_wp_theme_features; // Bail if the feature is disabled. if ( ! isset( $_wp_theme_features['view-transitions'] ) ) { return; } + // Bail if the current theme explicitly supports view transitions and the option to override is turned off. + $options = plvt_get_stored_setting_value(); + if ( $plvt_has_theme_support_with_args && ! $options['override_theme_config'] ) { + return; + } + $args = $_wp_theme_features['view-transitions']; // Apply the settings. - $options = plvt_get_stored_setting_value(); $args['default-animation'] = $options['default_transition_animation']; $selector_options = array( 'global' => array( @@ -233,19 +251,33 @@ function plvt_apply_settings_to_theme_support(): void { * * @since 1.0.0 * @access private + * + * @global bool|null $plvt_has_theme_support_with_args Whether the current theme explicitly supports view transitions with custom config. */ function plvt_add_setting_ui(): void { + global $plvt_has_theme_support_with_args; + add_settings_section( 'plvt_view_transitions', _x( 'View Transitions', 'Settings section', 'view-transitions' ), static function (): void { + global $plvt_has_theme_support_with_args; ?>

- +

'info', + 'additional_classes' => array( 'inline' ), + ) + ); + } }, 'reading', array( @@ -254,44 +286,82 @@ static function (): void { ) ); + add_settings_section( + 'plvt_admin_view_transitions', + _x( 'Admin View Transitions', 'Settings section', 'view-transitions' ), + static function (): void { + ?> +

+ +

+ '
', + 'after_section' => '
', + ) + ); + $fields = array( + 'override_theme_config' => array( + 'section' => 'plvt_view_transitions', + 'title' => __( 'Override Theme Configuration', 'view-transitions' ), + 'description' => __( 'Override the theme provided configuration with the settings below.', 'view-transitions' ), + ), 'default_transition_animation' => array( + 'section' => 'plvt_view_transitions', 'title' => __( 'Default Transition Animation', 'view-transitions' ), 'description' => __( 'Choose the animation that is used for the default view transition type.', 'view-transitions' ), ), 'header_selector' => array( + 'section' => 'plvt_view_transitions', 'title' => __( 'Header Selector', 'view-transitions' ), 'description' => __( 'Provide the CSS selector to detect the global header element.', 'view-transitions' ), ), 'main_selector' => array( + 'section' => 'plvt_view_transitions', 'title' => __( 'Main Selector', 'view-transitions' ), 'description' => __( 'Provide the CSS selector to detect the global main element.', 'view-transitions' ), ), 'post_title_selector' => array( + 'section' => 'plvt_view_transitions', 'title' => __( 'Post Title Selector', 'view-transitions' ), 'description' => __( 'Provide the CSS selector to detect the post title element.', 'view-transitions' ), ), 'post_thumbnail_selector' => array( + 'section' => 'plvt_view_transitions', 'title' => __( 'Post Thumbnail Selector', 'view-transitions' ), 'description' => __( 'Provide the CSS selector to detect the post thumbnail element.', 'view-transitions' ), ), 'post_content_selector' => array( + 'section' => 'plvt_view_transitions', 'title' => __( 'Post Content Selector', 'view-transitions' ), 'description' => __( 'Provide the CSS selector to detect the post content element.', 'view-transitions' ), ), 'enable_admin_transitions' => array( + 'section' => 'plvt_admin_view_transitions', 'title' => __( 'WP Admin', 'view-transitions' ), 'description' => __( 'Enable view transitions in the WordPress admin area.', 'view-transitions' ), ), ); + + // Do not render the checkbox to override if there is nothing to override. + if ( ! $plvt_has_theme_support_with_args ) { + unset( $fields['override_theme_config'] ); + } + foreach ( $fields as $slug => $args ) { + $section = $args['section']; + unset( $args['section'] ); + $additional_args = array( 'field' => $slug, 'label_for' => "plvt-view-transitions-field-{$slug}", ); - // Remove 'label_for' for checkbox field to avoid duplicate label association. - if ( 'enable_admin_transitions' === $slug ) { + // Remove 'label_for' for checkbox fields to avoid duplicate label association. + if ( 'override_theme_config' === $slug || 'enable_admin_transitions' === $slug ) { unset( $additional_args['label_for'] ); } @@ -300,7 +370,7 @@ static function (): void { $args['title'], 'plvt_render_settings_field', 'reading', - 'plvt_view_transitions', + $section, array_merge( $additional_args, $args @@ -332,6 +402,7 @@ function plvt_render_settings_field( array $args ): void { $type = 'select'; $choices = plvt_get_view_transition_animation_labels(); break; + case 'override_theme_config': case 'enable_admin_transitions': $type = 'checkbox'; $choices = array(); // Defined just for consistency. diff --git a/plugins/view-transitions/includes/theme.php b/plugins/view-transitions/includes/theme.php index 4722ff5206..be46b0f8f8 100644 --- a/plugins/view-transitions/includes/theme.php +++ b/plugins/view-transitions/includes/theme.php @@ -26,11 +26,21 @@ * * @since 1.0.0 * @access private + * + * @global bool|null $plvt_has_theme_support_with_args Whether the current theme explicitly supports view transitions with custom config. + * @global array $_wp_theme_features Theme support features added and their arguments. */ function plvt_polyfill_theme_support(): void { + global $plvt_has_theme_support_with_args, $_wp_theme_features; + if ( current_theme_supports( 'view-transitions' ) ) { + // If the current theme actually supports view transitions with a custom config, set a flag to inform the user. + if ( isset( $_wp_theme_features['view-transitions'] ) && true !== $_wp_theme_features['view-transitions'] ) { + $plvt_has_theme_support_with_args = true; + } return; } + add_theme_support( 'view-transitions' ); }