| 1 |
<?php |
| 2 |
|
| 3 |
// If this file is called directly, abort. |
| 4 |
if ( ! defined( 'WPINC' ) ) { |
| 5 |
die; |
| 6 |
} |
| 7 |
|
| 8 |
|
| 9 |
/** |
| 10 |
* Overloads the DOM with a notification if the Translation API is overloaded. |
| 11 |
* |
| 12 |
* This function modifies the DOM to include a notification message when the |
| 13 |
* Translation API is overloaded. It checks the provided arguments and user |
| 14 |
* permissions before making changes to the DOM. |
| 15 |
* |
| 16 |
* @param object $dom The DOM object to be modified. |
| 17 |
* @param array $args An array of arguments, including 'load' and 'overloaded' keys. |
| 18 |
* @return object The modified DOM object. |
| 19 |
*/ |
| 20 |
function wplng_dom_load_overload( $dom, $args ) { |
| 21 |
|
| 22 |
wplng_args_setup( $args ); |
| 23 |
|
| 24 |
if ( empty( $args['overloaded'] ) |
| 25 |
|| ! current_user_can( 'edit_posts' ) |
| 26 |
) { |
| 27 |
return $dom; |
| 28 |
} |
| 29 |
|
| 30 |
$html = '<div id="wplng-overloaded-container">'; |
| 31 |
|
| 32 |
$html .= '<span class="dashicons dashicons-info-outline"></span> '; |
| 33 |
|
| 34 |
$html .= '<span id="wplng-overloaded-text-mobile">'; |
| 35 |
$html .= esc_html__( 'Translation API overloaded', 'wplingua' ); |
| 36 |
$html .= '</span>'; |
| 37 |
|
| 38 |
$api_data = wplng_get_api_data(); |
| 39 |
|
| 40 |
if ( empty( $api_data['status'] ) || $api_data['status'] === 'FREE' ) { |
| 41 |
$html .= '<span id="wplng-overloaded-text-desktop">'; |
| 42 |
$html .= esc_html__( 'Translation API overloaded. Retry in a few minutes or ', 'wplingua' ); |
| 43 |
$html .= '<a href="https://wplingua.com/pricing/" target="_blank" rel="noopener noreferrer">'; |
| 44 |
$html .= esc_html__( 'upgrade your API key', 'wplingua' ); |
| 45 |
$html .= '</a>'; |
| 46 |
$html .= esc_html__( ' to get priority API access.', 'wplingua' ); |
| 47 |
$html .= '</span>'; |
| 48 |
} else { |
| 49 |
$html .= '<span id="wplng-overloaded-text-desktop">'; |
| 50 |
$html .= esc_html__( 'Translation API overloaded. Please retry in a few minutes.', 'wplingua' ); |
| 51 |
$html .= '</span>'; |
| 52 |
} |
| 53 |
|
| 54 |
$html .= '<span'; |
| 55 |
$html .= ' id="wplng-overloaded-close"'; |
| 56 |
$html .= ' class="dashicons dashicons-no-alt"'; |
| 57 |
$html .= ' title="' . esc_attr__( 'Close', 'wplingua' ) . '"'; |
| 58 |
$html .= '></span>'; |
| 59 |
|
| 60 |
$html .= '</div>'; // End #wplng-overloaded-container |
| 61 |
|
| 62 |
foreach ( $dom->find( 'body' ) as $body ) { |
| 63 |
|
| 64 |
// Add body clas .wplingua-api-overloaded |
| 65 |
if ( isset( $body->class ) ) { |
| 66 |
$body->class .= ' wplingua-api-overloaded'; |
| 67 |
} else { |
| 68 |
$body->class = 'wplingua-api-overloaded'; |
| 69 |
} |
| 70 |
|
| 71 |
// Add "API overloaded" HTML bar to body |
| 72 |
$body->innertext .= $html; |
| 73 |
} |
| 74 |
|
| 75 |
return $dom; |
| 76 |
} |
| 77 |
|