| @@ -1,5 +1,21 @@ | ||
| 1 | +/** | |
| 2 | + * WordPress dependencies | |
| 3 | + */ | |
| 4 | +import { escapeHTML } from '@wordpress/escape-html'; | |
| 5 | +import { __, sprintf } from '@wordpress/i18n'; | |
| 6 | + | |
| 7 | +/** | |
| 8 | + * Internal dependencies | |
| 9 | + */ | |
| 10 | +import { | |
| 11 | + ContentHelperError, | |
| 12 | + ContentHelperErrorCode, | |
| 13 | +} from '../content-helper/common/content-helper-error'; | |
| 14 | +import { CheckAuthProvider } from '../content-helper/common/providers/check-auth-provider'; | |
| 15 | + | |
| 1 | 16 | document.addEventListener( 'DOMContentLoaded', (): void => { |
| 17 | + displayContentHelperSectionMessages(); | |
| 2 | 18 | addContentHelperTabEventHandlers(); |
| 3 | 19 | setActiveTab(); |
| 4 | 20 | window.addEventListener( 'hashchange', setActiveTab ); |
| 5 | 21 | document.querySelector( '.media-single-image button.browse' )?.addEventListener( 'click', selectImage ); |
| @@ -54,9 +70,9 @@ | ||
| 54 | 70 | imageFrame.open(); |
| 55 | 71 | } |
| 56 | 72 | |
| 57 | 73 | /** |
| 58 | - * Adds the necessary event handlers to the Content Helper tab. | |
| 74 | + * Adds the necessary event handlers to the Content Intelligence tab. | |
| 59 | 75 | * |
| 60 | 76 | * @since 3.16.0 |
| 61 | 77 | */ |
| 62 | 78 | function addContentHelperTabEventHandlers(): void { |
| @@ -68,14 +84,15 @@ | ||
| 68 | 84 | // Selector for the checkboxes that enable/disable individual AI features. |
| 69 | 85 | const featureCheckboxes = document.querySelectorAll<HTMLInputElement>( |
| 70 | 86 | 'input#content_helper_smart_linking_enabled, ' + |
| 71 | 87 | 'input#content_helper_title_suggestions_enabled, ' + |
| 72 | - 'input#content_helper_excerpt_suggestions_enabled' | |
| 88 | + 'input#content_helper_excerpt_suggestions_enabled, ' + | |
| 89 | + 'input#content_helper_traffic_boost_enabled' | |
| 73 | 90 | ); |
| 74 | 91 | |
| 75 | - // Selector for all fieldsets in the Content Helper section. | |
| 92 | + // Selector for all fieldsets in the Content Intelligence section. | |
| 76 | 93 | const fieldsets = document.querySelectorAll( |
| 77 | - 'div.content-helper-section fieldset' | |
| 94 | + 'div.content-intelligence-section fieldset' | |
| 78 | 95 | ); |
| 79 | 96 | |
| 80 | 97 | // Event handlers. |
| 81 | 98 | enableAllFormFieldsOnSubmit(); |
| @@ -202,9 +219,9 @@ | ||
| 202 | 219 | */ |
| 203 | 220 | function enableAllFormFieldsOnSubmit(): void { |
| 204 | 221 | document.querySelector( '.wp-admin form[name="parsely"]' ) |
| 205 | 222 | ?.addEventListener( 'submit', (): void => { |
| 206 | - const baseSelector = '.wp-admin .content-helper-section fieldset'; | |
| 223 | + const baseSelector = '.wp-admin .content-intelligence-section fieldset'; | |
| 207 | 224 | |
| 208 | 225 | document.querySelectorAll( `${ baseSelector }[disabled]` ) |
| 209 | 226 | .forEach( ( fieldset: Element ): void => { |
| 210 | 227 | // Style the whole setting table row as disabled. |
| @@ -221,6 +238,64 @@ | ||
| 221 | 238 | |
| 222 | 239 | fieldset.removeAttribute( 'disabled' ); |
| 223 | 240 | } ); |
| 224 | 241 | } ); |
| 242 | + } | |
| 243 | +} | |
| 244 | + | |
| 245 | +/** | |
| 246 | + * Displays any messages needed under the Content Intelligence section. | |
| 247 | + * | |
| 248 | + * @since 3.19.0 | |
| 249 | + */ | |
| 250 | +async function displayContentHelperSectionMessages(): Promise<void> { | |
| 251 | + let message = null; | |
| 252 | + let authResponse = null; | |
| 253 | + | |
| 254 | + try { | |
| 255 | + const [ apiAuth, trafficBoostAuth ] = await Promise.all( [ | |
| 256 | + CheckAuthProvider.getInstance().getAuthorizationResponse( | |
| 257 | + { auth_scope: 'suggestions_api' } | |
| 258 | + ), | |
| 259 | + CheckAuthProvider.getInstance().getAuthorizationResponse( | |
| 260 | + { auth_scope: 'traffic_boost' } | |
| 261 | + ), | |
| 262 | + ] ); | |
| 263 | + | |
| 264 | + authResponse = { | |
| 265 | + api: apiAuth, | |
| 266 | + traffic_boost: trafficBoostAuth, | |
| 267 | + }; | |
| 268 | + } catch ( err: unknown ) { | |
| 269 | + console.error( err ); // eslint-disable-line no-console | |
| 270 | + | |
| 271 | + if ( err instanceof ContentHelperError ) { | |
| 272 | + if ( ContentHelperErrorCode.PluginSettingsApiSecretNotSet === err.code ) { | |
| 273 | + message = sprintf( '<p><strong>%s</strong></p>', escapeHTML( __( 'All Content Intelligence AI functionality is disabled because an API Secret has not been set.', 'wp-parsely' ) ) ); | |
| 274 | + } | |
| 275 | + } | |
| 276 | + } finally { | |
| 277 | + if ( authResponse ) { | |
| 278 | + if ( 200 !== authResponse.api.code ) { | |
| 279 | + const requestAccessLink = sprintf( '<a href="%1$s" target="_blank" rel="noopener">%2$s</a>', 'https://wpvip.com/content-helper/#content-helper-form', __( 'Request access here', 'wp-parsely' ) ); | |
| 280 | + /* translators: %s: Link to request access to Content Intelligence AI functionality. */ | |
| 281 | + const messageWithAccessLink = sprintf( escapeHTML( __( 'All Content Intelligence AI functionality is disabled for this website. %s.', 'wp-parsely' ) ), requestAccessLink ); | |
| 282 | + message = sprintf( '<p><strong>%s</strong></p>', messageWithAccessLink ); | |
| 283 | + } else if ( 200 === authResponse.api.code && 200 !== authResponse.traffic_boost.code ) { | |
| 284 | + const contactSupportLink = sprintf( '<a href="%1$s">%2$s</a>', 'mailto:[email protected]', '[email protected]' ); | |
| 285 | + /* translators: %s: Link to request access to Content Intelligence AI functionality. */ | |
| 286 | + const messageWithAccessLink = sprintf( escapeHTML( __( 'Engagement Boost functionality is disabled for this website. To enable it, contact %s.', 'wp-parsely' ) ), contactSupportLink ); | |
| 287 | + message = sprintf( '<p><strong>%s</strong></p>', messageWithAccessLink ); | |
| 288 | + } | |
| 289 | + } | |
| 290 | + | |
| 291 | + if ( message ) { | |
| 292 | + const div = document.createElement( 'div' ); | |
| 293 | + div.className = 'content-helper-message notice notice-error'; | |
| 294 | + div.innerHTML = message; | |
| 295 | + const contentHelperSection = document.querySelector( '.content-intelligence-section' ); | |
| 296 | + if ( contentHelperSection ) { | |
| 297 | + contentHelperSection.insertBefore( div, contentHelperSection.firstChild ); | |
| 298 | + } | |
| 299 | + } | |
| 225 | 300 | } |
| 226 | 301 | } |