Skip to content

Commit a22f06d

Browse files
authored
fix(media): move default image UI to Advanced Settings (#4296)
* fix(media): move default image UI to Advanced Settings * fix: image fallback setting title
1 parent 583b977 commit a22f06d

File tree

5 files changed

+37
-51
lines changed

5 files changed

+37
-51
lines changed

includes/class-default-image.php

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ class Default_Image {
2727
* Add hooks.
2828
*/
2929
public static function init() {
30-
add_filter( 'attachment_fields_to_save', [ __CLASS__, 'save_attachement_settings' ], 10, 2 );
31-
add_filter( 'attachment_fields_to_edit', [ __CLASS__, 'add_attachment_field' ], 10, 2 );
3230
add_action( 'template_redirect', [ __CLASS__, 'handle_not_found_image' ] );
3331
add_action( 'wp_footer', [ __CLASS__, 'add_404_image_handler_function' ] );
3432
}
@@ -50,44 +48,6 @@ public static function handle_not_found_image() {
5048
}
5149
}
5250

53-
/**
54-
* Add the relevant setting in the media editing screen.
55-
*
56-
* @param array $fields Array of media editor field info.
57-
* @param WP_Post $post Post object for current attachment.
58-
* @return array Modified $fields.
59-
*/
60-
public static function add_attachment_field( $fields, $post ) {
61-
$field_name = 'attachments[' . $post->ID . '][' . self::FIELD_NAME . ']';
62-
$value = ( wp_get_attachment_url( $post->ID ) === get_option( self::OPTION_NAME ) );
63-
$fields[ self::FIELD_NAME ] = [
64-
'label' => __( 'Is default image?', 'newspack-image-credits' ),
65-
'input' => 'html',
66-
'html' => '<input id="' . $field_name . '" name="' . $field_name . '" type="hidden" value="0" /><input id="' . $field_name . '" name="' . $field_name . '" type="checkbox" value="1" ' . checked( $value, true, false ) . ' />',
67-
'helps' => __( 'Use this image as a default image, if the requested one is not found (404 status).', 'newspack-plugin' ),
68-
];
69-
return $fields;
70-
}
71-
72-
/**
73-
* Save the relevant setting.
74-
*
75-
* @param array $post Array of post info.
76-
* @param array $attachment Array of media field input info.
77-
* @return array $post Unmodified post info.
78-
*/
79-
public static function save_attachement_settings( $post, $attachment ) {
80-
if ( isset( $attachment[ self::FIELD_NAME ] ) ) {
81-
if ( '1' === $attachment[ self::FIELD_NAME ] ) {
82-
update_option( self::OPTION_NAME, wp_get_attachment_url( $post['ID'] ) );
83-
} else {
84-
delete_option( self::OPTION_NAME );
85-
}
86-
}
87-
88-
return $post;
89-
}
90-
9151
/**
9252
* Add JavaScript function to handle 404 images.
9353
* If an image is served from a different domain, the `template_redirect` action won't catch a 404.
@@ -98,7 +58,7 @@ public static function add_404_image_handler_function() {
9858
if ( ! empty( $default_image_url ) ) {
9959
?>
10060
<script>
101-
document.querySelectorAll('img').forEach(function(imgEl) {
61+
document.querySelectorAll('.content-area img').forEach(function(imgEl) {
10262
imgEl.addEventListener('error', function(){
10363
if (!imgEl.hasAttribute('data-404-handled')) {
10464
imgEl.setAttribute('data-404-handled', 'true');

includes/wizards/class-setup-wizard.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,9 @@ public function api_retrieve_theme_and_set_defaults() {
384384
// Append PWA display mode option.
385385
$theme_mods['pwa_display_mode'] = get_option( 'newspack_pwa_display_mode', 'minimal-ui' );
386386

387+
// Append post content fallback image option.
388+
$theme_mods['post_content_fallback_image'] = get_option( Default_Image::OPTION_NAME, null );
389+
387390
return rest_ensure_response(
388391
[
389392
'theme' => Starter_Content::get_theme(),
@@ -530,6 +533,12 @@ public function api_update_theme_with_mods( $request ) {
530533
continue;
531534
}
532535

536+
// Post content fallback image is an option, not a theme mod.
537+
if ( 'post_content_fallback_image' === $key ) {
538+
update_option( Default_Image::OPTION_NAME, $value );
539+
continue;
540+
}
541+
533542
// All-posts updates: featured image and post template.
534543
if ( substr_compare( $key, '_all_posts', -strlen( '_all_posts' ) ) === 0 ) {
535544
if ( 'none' === $value ) {

src/wizards/newspack/views/settings/advanced-settings/index.tsx

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ import { useEffect } from '@wordpress/element';
1111
/**
1212
* Internal dependencies.
1313
*/
14-
import { DEFAULT_THEME_MODS } from '../constants';
14+
import { ADVANCED_SETTINGS_DEFAULTS } from '../constants';
1515
import WizardsTab from '../../../../wizards-tab';
1616
import WizardSection from '../../../../wizards-section';
17-
import { Button, hooks, Notice, utils } from '../../../../../../packages/components/src';
17+
import { Button, Grid, hooks, ImageUpload, Notice, utils } from '../../../../../../packages/components/src';
1818
import { useWizardApiFetch } from '../../../../hooks/use-wizard-api-fetch';
1919
import Recirculation from './recirculation';
2020
import AuthorBio from './author-bio';
@@ -26,7 +26,7 @@ import PwaDisplayMode from './pwa-display-mode';
2626

2727
export default function AdvancedSettings() {
2828
const [ data, setData ] = hooks.useObjectState< AdvancedSettings >( {
29-
...DEFAULT_THEME_MODS,
29+
...ADVANCED_SETTINGS_DEFAULTS,
3030
} );
3131
const [ etc, setEtc ] = hooks.useObjectState< Etc >( {
3232
post_count: '0',
@@ -124,7 +124,7 @@ export default function AdvancedSettings() {
124124
<AuthorBio update={ setData } data={ data } isFetching={ isFetching } />
125125
</WizardSection>
126126
<WizardSection
127-
title={ __( 'Default Featured Image Position And Post Template', 'newspack-plugin' ) }
127+
title={ __( 'Default Featured Image Position and Post Template', 'newspack-plugin' ) }
128128
description={ __( 'Modify how the featured image and post template settings are applied to new posts.', 'newspack-plugin' ) }
129129
>
130130
<FeaturedImagePostsNew data={ data } update={ setData } />
@@ -141,6 +141,18 @@ export default function AdvancedSettings() {
141141
<WizardSection title={ __( 'Media Credits', 'newspack-plugin' ) }>
142142
<MediaCredits data={ data } update={ setData } />
143143
</WizardSection>
144+
<WizardSection
145+
title={ __( 'Image fallback', 'newspack-plugin' ) }
146+
description={ __( 'Select a fallback image to display when an image inside post content cannot be found.', 'newspack-plugin' ) }
147+
>
148+
<Grid>
149+
<ImageUpload
150+
image={ data.post_content_fallback_image ? { url: data.post_content_fallback_image } : null }
151+
buttonLabel={ __( 'Select', 'newspack-plugin' ) }
152+
onChange={ ( image: null | PlaceholderImage ) => setData( { post_content_fallback_image: image?.url || null } ) }
153+
/>
154+
</Grid>
155+
</WizardSection>
144156
<WizardSection>
145157
<AccessibilityStatement isFetching={ isFetching } />
146158
</WizardSection>

src/wizards/newspack/views/settings/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ export const ADVANCED_SETTINGS_DEFAULTS = {
6060
newspack_image_credits_auto_populate: false,
6161
// PWA Display Mode.
6262
pwa_display_mode: 'minimal-ui',
63+
// Post content fallback image.
64+
newspack_default_image_url: undefined,
6365
};
6466

6567
export const DEFAULT_THEME_MODS: ThemeMods = {

src/wizards/newspack/views/settings/theme-mods.d.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ type ThemeNames = 'theme' | 'scott' | 'nelson' | 'katharine' | 'sacha' | 'joseph
66
/**
77
* Theme names with `newspack` prefix.
88
*/
9-
type NewspackThemes = `newspack-${ ThemeNames }`;
9+
type NewspackThemes = `newspack-${ThemeNames}`;
1010

1111
/**
1212
* Property on theme mods endpoint.
@@ -19,10 +19,10 @@ interface Etc {
1919
/**
2020
* Theme and brand schema.
2121
*/
22-
interface ThemeData< T = {} > {
22+
interface ThemeData<T = {}> {
2323
etc: Etc;
2424
theme: '' | NewspackThemes;
25-
theme_mods: ThemeMods< T >;
25+
theme_mods: ThemeMods<T>;
2626
homepage_patterns: HomepagePattern[];
2727
}
2828

@@ -77,8 +77,8 @@ interface ThemeAndBrand {
7777
/**
7878
* Theme mods component.
7979
*/
80-
type ThemeModComponentProps< T = ThemeMods > = {
81-
update: ( a: Partial< T > ) => void;
80+
type ThemeModComponentProps<T = ThemeMods> = {
81+
update: (a: Partial<T>) => void;
8282
isFetching?: boolean;
8383
data: T;
8484
};
@@ -126,6 +126,9 @@ interface AdvancedSettings {
126126

127127
// PWA Display Mode.
128128
pwa_display_mode: string;
129+
130+
// Post content fallback image.
131+
post_content_fallback_image?: string | null;
129132
}
130133

131134
interface MiscSettings {
@@ -135,4 +138,4 @@ interface MiscSettings {
135138
custom_css_post_id: number;
136139
}
137140

138-
interface ThemeMods extends ThemeAndBrand, AdvancedSettings, MiscSettings {}
141+
interface ThemeMods extends ThemeAndBrand, AdvancedSettings, MiscSettings { }

0 commit comments

Comments
 (0)