| 1 |
/** |
| 2 |
* WordPress dependencies |
| 3 |
*/ |
| 4 |
import { __ } from '@wordpress/i18n'; |
| 5 |
import { CheckboxControl } from '@wordpress/components'; |
| 6 |
import { compose } from '@wordpress/compose'; |
| 7 |
import { withDispatch, withSelect } from '@wordpress/data'; |
| 8 |
import { Fragment, useEffect } from '@wordpress/element'; |
| 9 |
|
| 10 |
/** |
| 11 |
* Internal dependencies |
| 12 |
*/ |
| 13 |
import GenerateAudioCheck from './check'; |
| 14 |
|
| 15 |
export function GenerateAudio( { |
| 16 |
generateAudio, |
| 17 |
generateAudioEdited, |
| 18 |
setGenerateAudio, |
| 19 |
wrapper, |
| 20 |
} ) { |
| 21 |
const Wrapper = wrapper || Fragment; |
| 22 |
|
| 23 |
// Set "Generate audio" to "1" in the store when it has been preselected |
| 24 |
useEffect( () => { |
| 25 |
if ( ! generateAudioEdited && generateAudio ) { |
| 26 |
setGenerateAudio( generateAudio ); |
| 27 |
} |
| 28 |
}, [ generateAudioEdited, generateAudio ] ); |
| 29 |
|
| 30 |
return ( |
| 31 |
<GenerateAudioCheck> |
| 32 |
<Wrapper> |
| 33 |
<CheckboxControl |
| 34 |
className="beyondwords--generate-audio" |
| 35 |
label={ __( 'Generate audio', 'speechkit' ) } |
| 36 |
checked={ generateAudio } |
| 37 |
onChange={ () => { |
| 38 |
setGenerateAudio( ! generateAudio ); |
| 39 |
} } |
| 40 |
/> |
| 41 |
</Wrapper> |
| 42 |
</GenerateAudioCheck> |
| 43 |
); |
| 44 |
} |
| 45 |
|
| 46 |
export default compose( [ |
| 47 |
withSelect( ( select ) => { |
| 48 |
// const { getEntityRecords } = select( 'core' ); |
| 49 |
|
| 50 |
const { |
| 51 |
getCurrentPostAttribute, |
| 52 |
getCurrentPostType, |
| 53 |
getEditedPostAttribute, |
| 54 |
getPostEdits, |
| 55 |
} = select( 'core/editor' ); |
| 56 |
|
| 57 |
const { getGenerateAudioEdited } = select( 'beyondwords/interactions' ); |
| 58 |
const { getSettings } = select( 'beyondwords/settings' ); |
| 59 |
|
| 60 |
/** |
| 61 |
* Get the Generate audio value. |
| 62 |
* |
| 63 |
* This is a little complex because it is also controlled (auto-checked/unchecked) |
| 64 |
* based on the assigned Categories, and we need to be able to override it. |
| 65 |
*/ |
| 66 |
const getGenerateAudio = () => { |
| 67 |
const { meta } = getPostEdits(); |
| 68 |
|
| 69 |
// Has "Generate audio" been edited in this session (manually checked or unchecked)? |
| 70 |
if ( |
| 71 |
getGenerateAudioEdited() && |
| 72 |
meta && |
| 73 |
'beyondwords_generate_audio' in meta |
| 74 |
) { |
| 75 |
return meta.beyondwords_generate_audio === '1'; |
| 76 |
} |
| 77 |
|
| 78 |
// Check various custom fields in the saved post |
| 79 |
const { |
| 80 |
/* eslint-disable-next-line camelcase */ |
| 81 |
beyondwords_generate_audio, |
| 82 |
/* eslint-disable-next-line camelcase */ |
| 83 |
speechkit_generate_audio, |
| 84 |
/* eslint-disable-next-line camelcase */ |
| 85 |
publish_post_to_speechkit, |
| 86 |
} = getCurrentPostAttribute( 'meta' ); |
| 87 |
|
| 88 |
if ( |
| 89 |
/* eslint-disable-next-line camelcase */ |
| 90 |
beyondwords_generate_audio === '1' || |
| 91 |
/* eslint-disable-next-line camelcase */ |
| 92 |
speechkit_generate_audio === '1' || |
| 93 |
/* eslint-disable-next-line camelcase */ |
| 94 |
publish_post_to_speechkit === '1' |
| 95 |
) { |
| 96 |
return true; |
| 97 |
} |
| 98 |
|
| 99 |
if ( |
| 100 |
/* eslint-disable-next-line camelcase */ |
| 101 |
beyondwords_generate_audio === '0' || |
| 102 |
/* eslint-disable-next-line camelcase */ |
| 103 |
speechkit_generate_audio === '0' || |
| 104 |
/* eslint-disable-next-line camelcase */ |
| 105 |
publish_post_to_speechkit === '0' |
| 106 |
) { |
| 107 |
return false; |
| 108 |
} |
| 109 |
|
| 110 |
return null; |
| 111 |
}; |
| 112 |
|
| 113 |
/** |
| 114 |
* Should we preselect "Generate audio", based on the plugin setting? |
| 115 |
*/ |
| 116 |
const getShouldPreselect = () => { |
| 117 |
const settings = getSettings(); |
| 118 |
|
| 119 |
// Do we have settings? |
| 120 |
if ( ! settings ) { |
| 121 |
return false; |
| 122 |
} |
| 123 |
|
| 124 |
const preselect = |
| 125 |
typeof settings.preselect === 'object' && |
| 126 |
settings.preselect !== null |
| 127 |
? settings.preselect |
| 128 |
: {}; |
| 129 |
|
| 130 |
const postType = getCurrentPostType(); |
| 131 |
|
| 132 |
// Exit if the current post type does not exist in the plugin settings |
| 133 |
if ( false === postType in preselect ) { |
| 134 |
return false; |
| 135 |
} |
| 136 |
|
| 137 |
// Is the current post type checked in the plugin settings |
| 138 |
// If it is checked at post-level then we preselect Generate audio regardless |
| 139 |
// of the applied taxonomies |
| 140 |
if ( preselect[ postType ] === '1' ) { |
| 141 |
return true; |
| 142 |
} |
| 143 |
|
| 144 |
// Get the Post edits |
| 145 |
const postEdits = getPostEdits(); |
| 146 |
|
| 147 |
// Check that categories have been edited? |
| 148 |
// todo support multiple taxonomies |
| 149 |
if ( ! Array.isArray( postEdits.categories ) ) { |
| 150 |
return false; |
| 151 |
} |
| 152 |
|
| 153 |
// Handle cases where preselect[ postType ] is not an object |
| 154 |
// This can happen when the plugin setting is empty or corrupt |
| 155 |
if ( |
| 156 |
typeof preselect[ postType ] !== 'object' || |
| 157 |
preselect[ postType ] === null |
| 158 |
) { |
| 159 |
return false; |
| 160 |
} |
| 161 |
|
| 162 |
// Get all Post categories |
| 163 |
const categories = getEditedPostAttribute( 'categories' ); |
| 164 |
|
| 165 |
// Do any Post categories match the plugin settings? |
| 166 |
const hasMatchingCategories = categories.some( ( x ) => { |
| 167 |
// todo support multiple taxonomies |
| 168 |
if ( false === 'category' in preselect[ postType ] ) { |
| 169 |
return false; |
| 170 |
} |
| 171 |
// todo support multiple taxonomies |
| 172 |
return preselect[ postType ].category.includes( String( x ) ); |
| 173 |
} ); |
| 174 |
|
| 175 |
if ( hasMatchingCategories ) { |
| 176 |
return true; |
| 177 |
} |
| 178 |
|
| 179 |
// todo Do any Post OTHER TAXONOMIES match the plugin settings? |
| 180 |
|
| 181 |
return false; |
| 182 |
}; |
| 183 |
|
| 184 |
const generateAudio = getGenerateAudio(); |
| 185 |
|
| 186 |
return { |
| 187 |
generateAudio: |
| 188 |
generateAudio === null ? getShouldPreselect() : generateAudio, |
| 189 |
generateAudioEdited: getGenerateAudioEdited(), |
| 190 |
}; |
| 191 |
} ), |
| 192 |
withDispatch( ( dispatch ) => { |
| 193 |
const { editPost } = dispatch( 'core/editor' ); |
| 194 |
const { setGenerateAudioEdited } = dispatch( |
| 195 |
'beyondwords/interactions' |
| 196 |
); |
| 197 |
|
| 198 |
return { |
| 199 |
setGenerateAudio: ( generateAudio ) => { |
| 200 |
// Update the Post Meta (AKA the Custom Field) |
| 201 |
editPost( { |
| 202 |
meta: { |
| 203 |
/* eslint-disable-next-line camelcase */ |
| 204 |
beyondwords_generate_audio: generateAudio ? '1' : '0', |
| 205 |
}, |
| 206 |
} ); |
| 207 |
// Mark "Generate audio" as being (manually) edited, so other components |
| 208 |
// know the checkbox has been chnaged from it's default value. |
| 209 |
setGenerateAudioEdited( true ); |
| 210 |
}, |
| 211 |
}; |
| 212 |
} ), |
| 213 |
] )( GenerateAudio ); |
| 214 |
|