| 1 |
import { __ } from '@wordpress/i18n'; |
| 2 |
import { escapeHTML } from '@wordpress/escape-html'; |
| 3 |
import { formatFileSize } from '../../utils'; |
| 4 |
import { UPGRADE_LINK } from '../../constants'; |
| 5 |
|
| 6 |
const notOptimizedTemplate = () => { |
| 7 |
return ` |
| 8 |
<p class="image-optimization-control__property"> |
| 9 |
${ __( 'Status', 'image-optimization' ) }: |
| 10 |
|
| 11 |
<span class="image-optimization-control__property-value"> |
| 12 |
${ __( 'Not optimized', 'image-optimization' ) } |
| 13 |
</span> |
| 14 |
</p> |
| 15 |
|
| 16 |
<div class="image-optimization-control__action-button-wrapper"> |
| 17 |
<button type="button" |
| 18 |
class="button button-primary image-optimization-control__button image-optimization-control__button--optimize"> |
| 19 |
${ __( 'Optimize now', 'image-optimization' ) } |
| 20 |
</button> |
| 21 |
</div> |
| 22 |
`; |
| 23 |
}; |
| 24 |
|
| 25 |
const loadingTemplate = () => { |
| 26 |
return ` |
| 27 |
<p class="image-optimization-control__property"> |
| 28 |
${ __( 'Status', 'image-optimization' ) }: |
| 29 |
|
| 30 |
<span class="image-optimization-control__property-value"> |
| 31 |
${ __( 'In Progress', 'image-optimization' ) } |
| 32 |
</span> |
| 33 |
</p> |
| 34 |
|
| 35 |
<div class="image-optimization-control__action-spinner-wrapper"> |
| 36 |
<span class="spinner is-active"></span> |
| 37 |
</div> |
| 38 |
`; |
| 39 |
}; |
| 40 |
|
| 41 |
const errorTemplate = ( message, imagesLeft ) => { |
| 42 |
return ` |
| 43 |
<p class="image-optimization-control__property"> |
| 44 |
${ __( 'Status', 'image-optimization' ) }: |
| 45 |
|
| 46 |
<span class="image-optimization-control__property-value"> |
| 47 |
${ __( 'Error', 'image-optimization' ) } |
| 48 |
</span> |
| 49 |
</p> |
| 50 |
|
| 51 |
<p class="image-optimization-control__property"> |
| 52 |
${ __( 'Reason', 'image-optimization' ) }: |
| 53 |
|
| 54 |
<span class="image-optimization-control__property-value"> |
| 55 |
${ escapeHTML( message ) } |
| 56 |
</span> |
| 57 |
</p> |
| 58 |
|
| 59 |
<div class="image-optimization-control__action-button-wrapper"> |
| 60 |
${ imagesLeft === 0 |
| 61 |
? `<a class="button button-secondary button-large image-optimization-control__button" |
| 62 |
href="${ UPGRADE_LINK }" |
| 63 |
target="_blank" rel="noopener noreferrer"> |
| 64 |
${ __( 'Upgrade', 'image-optimization' ) } |
| 65 |
</a> |
| 66 |
` : ` |
| 67 |
<button class="button button-secondary button-large button-link-delete image-optimization-control__button image-optimization-control__button--try-again" |
| 68 |
type="button"> |
| 69 |
${ __( 'Try again', 'image-optimization' ) } |
| 70 |
</button>` } |
| 71 |
</div> |
| 72 |
`; |
| 73 |
}; |
| 74 |
|
| 75 |
const optimizedTemplate = ( data ) => { |
| 76 |
const absoluteValue = formatFileSize( data?.saved?.absolute, 1 ); |
| 77 |
|
| 78 |
return ` |
| 79 |
<p class="image-optimization-control__property"> |
| 80 |
${ __( 'Status', 'image-optimization' ) }: |
| 81 |
|
| 82 |
<span class="image-optimization-control__property-value"> |
| 83 |
${ __( 'Optimized', 'image-optimization' ) } |
| 84 |
</span> |
| 85 |
</p> |
| 86 |
|
| 87 |
<p class="image-optimization-control__property"> |
| 88 |
${ __( 'Image sizes optimized', 'image-optimization' ) }: |
| 89 |
|
| 90 |
<span class="image-optimization-control__property-value"> |
| 91 |
${ data?.sizesOptimized }/${ data?.sizesTotal } |
| 92 |
|
| 93 |
<button type="button" |
| 94 |
class="image-optimization-control__details-button" |
| 95 |
aria-label="${ __( 'Open optimization details', 'image-optimization' ) }"> |
| 96 |
(+) |
| 97 |
</button> |
| 98 |
</span> |
| 99 |
</p> |
| 100 |
|
| 101 |
<p class="image-optimization-control__property"> |
| 102 |
${ data?.saved?.absolute !== 0 |
| 103 |
? ` |
| 104 |
<span class="image-optimization-control__property-value"> |
| 105 |
${ __( 'Overall saving', 'image-optimization' ) }: ${ data?.saved?.relative }% (${ absoluteValue }) |
| 106 |
</span> |
| 107 |
` : ` |
| 108 |
<span class="image-optimization-control__property-value"> |
| 109 |
${ __( 'Image is fully optimized', 'image-optimization' ) } |
| 110 |
</span> |
| 111 |
` } |
| 112 |
</p> |
| 113 |
|
| 114 |
<div class="image-optimization-control__action-button-wrapper"> |
| 115 |
${ data?.canBeRestored ? ` |
| 116 |
<button class="button button-link image-optimization-control__button image-optimization-control__button--restore-original" |
| 117 |
type="button"> |
| 118 |
${ __( 'Restore original', 'image-optimization' ) } |
| 119 |
</button> |
| 120 |
` : '' } |
| 121 |
|
| 122 |
<button class="button button-link image-optimization-control__button image-optimization-control__button--reoptimize" |
| 123 |
type="button"> |
| 124 |
${ __( 'Reoptimize', 'image-optimization' ) } |
| 125 |
</button> |
| 126 |
</div> |
| 127 |
`; |
| 128 |
}; |
| 129 |
|
| 130 |
const metaBoxTemplates = Object.freeze( { |
| 131 |
notOptimizedTemplate, |
| 132 |
loadingTemplate, |
| 133 |
errorTemplate, |
| 134 |
optimizedTemplate, |
| 135 |
} ); |
| 136 |
|
| 137 |
export default metaBoxTemplates; |
| 138 |
|