| 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 |
$api_data = wplng_get_api_data(); |
| 31 |
|
| 32 |
$html = '<div id="wplng-overloaded-container">'; |
| 33 |
|
| 34 |
// Icon |
| 35 |
$html .= '<span class="dashicons dashicons-info-outline"></span> '; |
| 36 |
|
| 37 |
if ( ( $api_data['expired'] === true ) && ! empty( $api_data['plan'] ) && is_string( $api_data['plan'] ) ) { |
| 38 |
|
| 39 |
/** |
| 40 |
* wpLingua plan expired |
| 41 |
*/ |
| 42 |
|
| 43 |
// Link "Learn more" |
| 44 |
$html_link = '<a'; |
| 45 |
$html_link .= ' href="' . esc_url( admin_url( 'admin.php?page=wplingua-settings' ) ) . '"'; |
| 46 |
$html_link .= '>'; |
| 47 |
$html_link .= esc_html__( 'Learn more', 'wplingua' ); |
| 48 |
$html_link .= '</a>'; |
| 49 |
|
| 50 |
// Mobile text |
| 51 |
$html .= '<span id="wplng-overloaded-text-mobile">'; |
| 52 |
$html .= esc_html__( 'wpLingua plan expired.', 'wplingua' ); |
| 53 |
$html .= ' ' . $html_link; |
| 54 |
$html .= '</span>'; |
| 55 |
|
| 56 |
// Desktop text |
| 57 |
$html .= '<span id="wplng-overloaded-text-desktop">'; |
| 58 |
|
| 59 |
$html .= sprintf( |
| 60 |
/* translators: 1: plan name (STARTER or PREMIUM), 2: expiration date */ |
| 61 |
esc_html__( 'Your wpLingua %1$s plan expired on %2$s. Automatic translations are no longer available.', 'wplingua' ), |
| 62 |
esc_html( $api_data['plan'] ), |
| 63 |
esc_html( ! empty( $api_data['expiration'] ) ? $api_data['expiration'] : '' ) |
| 64 |
); |
| 65 |
|
| 66 |
$html .= ' ' . $html_link; |
| 67 |
|
| 68 |
$html .= '</span>'; |
| 69 |
|
| 70 |
} elseif ( empty( $api_data['status'] ) || $api_data['status'] === 'FREE' ) { |
| 71 |
|
| 72 |
/** |
| 73 |
* API overload message for FREE users |
| 74 |
*/ |
| 75 |
|
| 76 |
// Mobile text |
| 77 |
$html .= '<span id="wplng-overloaded-text-mobile">'; |
| 78 |
$html .= esc_html__( 'Translation API overloaded', 'wplingua' ); |
| 79 |
$html .= '</span>'; |
| 80 |
|
| 81 |
// Desktop text |
| 82 |
$html .= '<span id="wplng-overloaded-text-desktop">'; |
| 83 |
$html .= esc_html__( 'Translation API overloaded. Retry in a few minutes or ', 'wplingua' ); |
| 84 |
$html .= '<a href="https://wplingua.com/pricing/" target="_blank" rel="noopener noreferrer">'; |
| 85 |
$html .= esc_html__( 'upgrade your API key', 'wplingua' ); |
| 86 |
$html .= '</a>'; |
| 87 |
$html .= esc_html__( ' to get priority API access.', 'wplingua' ); |
| 88 |
$html .= '</span>'; |
| 89 |
} else { |
| 90 |
|
| 91 |
/** |
| 92 |
* API overload message for PRO users |
| 93 |
*/ |
| 94 |
|
| 95 |
// Mobile text |
| 96 |
$html .= '<span id="wplng-overloaded-text-mobile">'; |
| 97 |
$html .= esc_html__( 'Translation API overloaded', 'wplingua' ); |
| 98 |
$html .= '</span>'; |
| 99 |
|
| 100 |
// Desktop text |
| 101 |
$html .= '<span id="wplng-overloaded-text-desktop">'; |
| 102 |
$html .= esc_html__( 'Translation API overloaded. Please retry in a few minutes.', 'wplingua' ); |
| 103 |
$html .= '</span>'; |
| 104 |
} |
| 105 |
|
| 106 |
// Close icon / button |
| 107 |
$html .= '<span'; |
| 108 |
$html .= ' id="wplng-overloaded-close"'; |
| 109 |
$html .= ' class="dashicons dashicons-no-alt"'; |
| 110 |
$html .= ' title="' . esc_attr__( 'Close', 'wplingua' ) . '"'; |
| 111 |
$html .= '></span>'; |
| 112 |
|
| 113 |
$html .= '</div>'; // End #wplng-overloaded-container |
| 114 |
|
| 115 |
foreach ( $dom->find( 'body' ) as $body ) { |
| 116 |
|
| 117 |
// Add body clas .wplingua-api-overloaded |
| 118 |
if ( isset( $body->class ) ) { |
| 119 |
$body->class .= ' wplingua-api-overloaded'; |
| 120 |
} else { |
| 121 |
$body->class = 'wplingua-api-overloaded'; |
| 122 |
} |
| 123 |
|
| 124 |
// Add "API overloaded" HTML bar to body |
| 125 |
$body->innertext .= $html; |
| 126 |
} |
| 127 |
|
| 128 |
return $dom; |
| 129 |
} |
| 130 |
|