| 1 |
<?php |
| 2 |
declare( strict_types=1 ); |
| 3 |
|
| 4 |
namespace Imagify\Admin; |
| 5 |
|
| 6 |
use Imagify\EventManagement\SubscriberInterface; |
| 7 |
use Imagify\User\User; |
| 8 |
use Imagify_Views; |
| 9 |
use WP_Admin_Bar; |
| 10 |
|
| 11 |
/** |
| 12 |
* Admin bar handler |
| 13 |
*/ |
| 14 |
class AdminBar implements SubscriberInterface { |
| 15 |
/** |
| 16 |
* User instance. |
| 17 |
* |
| 18 |
* @var User |
| 19 |
*/ |
| 20 |
private $user; |
| 21 |
|
| 22 |
/** |
| 23 |
* AdminBar constructor. |
| 24 |
* |
| 25 |
* @param User $user User instance. |
| 26 |
*/ |
| 27 |
public function __construct( User $user ) { |
| 28 |
$this->user = $user; |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Returns an array of events this subscriber listens to |
| 33 |
* |
| 34 |
* @return array |
| 35 |
*/ |
| 36 |
public static function get_subscribed_events(): array { |
| 37 |
return [ |
| 38 |
// @action |
| 39 |
'wp_ajax_imagify_get_admin_bar_profile' => 'get_admin_bar_profile_callback', |
| 40 |
// @action |
| 41 |
'admin_bar_menu' => [ 'add_imagify_admin_bar_menu', IMAGIFY_INT_MAX ], |
| 42 |
]; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Add Imagify menu in the admin bar. |
| 47 |
* |
| 48 |
* @param WP_Admin_Bar $wp_admin_bar WP_Admin_Bar instance, passed by reference. |
| 49 |
*/ |
| 50 |
public function add_imagify_admin_bar_menu( $wp_admin_bar ) { |
| 51 |
if ( ! imagify_get_context( 'wp' )->current_user_can( 'manage' ) ) { |
| 52 |
return; |
| 53 |
} |
| 54 |
|
| 55 |
if ( ! get_imagify_option( 'admin_bar_menu' ) ) { |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
// Parent. |
| 60 |
$wp_admin_bar->add_menu( |
| 61 |
[ |
| 62 |
'id' => 'imagify', |
| 63 |
'title' => 'Imagify', |
| 64 |
'href' => get_imagify_admin_url(), |
| 65 |
] |
| 66 |
); |
| 67 |
|
| 68 |
// Settings. |
| 69 |
$wp_admin_bar->add_menu( |
| 70 |
[ |
| 71 |
'parent' => 'imagify', |
| 72 |
'id' => 'imagify-settings', |
| 73 |
'title' => __( 'Settings' ), |
| 74 |
'href' => get_imagify_admin_url(), |
| 75 |
] |
| 76 |
); |
| 77 |
|
| 78 |
// Bulk Optimization. |
| 79 |
if ( ! is_network_admin() ) { |
| 80 |
$wp_admin_bar->add_menu( |
| 81 |
[ |
| 82 |
'parent' => 'imagify', |
| 83 |
'id' => 'imagify-bulk-optimization', |
| 84 |
'title' => __( 'Bulk Optimization', 'imagify' ), |
| 85 |
'href' => get_imagify_admin_url( 'bulk-optimization' ), |
| 86 |
] |
| 87 |
); |
| 88 |
} |
| 89 |
|
| 90 |
// Documentation. |
| 91 |
$wp_admin_bar->add_menu( |
| 92 |
[ |
| 93 |
'parent' => 'imagify', |
| 94 |
'id' => 'imagify-documentation', |
| 95 |
'title' => __( 'Documentation', 'imagify' ), |
| 96 |
'href' => imagify_get_external_url( 'documentation' ), |
| 97 |
'meta' => [ |
| 98 |
'target' => '_blank', |
| 99 |
], |
| 100 |
] |
| 101 |
); |
| 102 |
|
| 103 |
// Rate it. |
| 104 |
$wp_admin_bar->add_menu( |
| 105 |
[ |
| 106 |
'parent' => 'imagify', |
| 107 |
'id' => 'imagify-rate-it', |
| 108 |
/* translators: %s is WordPress.org. */ |
| 109 |
'title' => sprintf( __( 'Rate Imagify on %s', 'imagify' ), 'WordPress.org' ), |
| 110 |
'href' => imagify_get_external_url( 'rate' ), |
| 111 |
'meta' => [ |
| 112 |
'target' => '_blank', |
| 113 |
], |
| 114 |
] |
| 115 |
); |
| 116 |
|
| 117 |
// Quota & Profile informations. |
| 118 |
if ( |
| 119 |
( defined( 'IMAGIFY_HIDDEN_ACCOUNT' ) && IMAGIFY_HIDDEN_ACCOUNT ) |
| 120 |
|| |
| 121 |
! get_imagify_option( 'api_key' ) |
| 122 |
) { |
| 123 |
return; |
| 124 |
} |
| 125 |
|
| 126 |
$wp_admin_bar->add_menu( |
| 127 |
[ |
| 128 |
'parent' => 'imagify', |
| 129 |
'id' => 'imagify-upgrade-plan', |
| 130 |
'title' => '<div id="wp-admin-bar-imagify-pricing-content" class="hide-if-no-js"></div>', |
| 131 |
] |
| 132 |
); |
| 133 |
|
| 134 |
$wp_admin_bar->add_menu( |
| 135 |
[ |
| 136 |
'parent' => 'imagify', |
| 137 |
'id' => 'imagify-profile', |
| 138 |
'title' => wp_nonce_field( 'imagify-get-admin-bar-profile', 'imagifygetadminbarprofilenonce', false, false ) . '<div id="wp-admin-bar-imagify-profile-loading" class="hide-if-no-js">' . __( 'Loading...', 'imagify' ) . '</div><div id="wp-admin-bar-imagify-profile-content" class="hide-if-no-js"></div>', |
| 139 |
] |
| 140 |
); |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Get admin bar profile output. |
| 145 |
* |
| 146 |
* @return void |
| 147 |
*/ |
| 148 |
public function get_admin_bar_profile_callback() { |
| 149 |
imagify_check_nonce( 'imagify-get-admin-bar-profile', 'imagifygetadminbarprofilenonce' ); |
| 150 |
|
| 151 |
if ( ! imagify_get_context( 'wp' )->current_user_can( 'manage' ) ) { |
| 152 |
imagify_die(); |
| 153 |
} |
| 154 |
|
| 155 |
$views = Imagify_Views::get_instance(); |
| 156 |
$unconsumed_quota = $views->get_quota_percent(); |
| 157 |
$text = ''; |
| 158 |
$button_text = ''; |
| 159 |
$upgrade_link = ''; |
| 160 |
|
| 161 |
if ( $this->user->is_free() ) { |
| 162 |
$text = esc_html__( 'Upgrade your plan now for more!', 'imagify' ) . '<br>' . |
| 163 |
esc_html__( 'From $5.99/month only, keep going with image optimization!', 'imagify' ); |
| 164 |
$button_text = esc_html__( 'Upgrade My Plan', 'imagify' ); |
| 165 |
$upgrade_link = IMAGIFY_APP_DOMAIN . '/subscription/?utm_source=plugin&utm_medium=notification'; |
| 166 |
} elseif ( $this->user->is_growth() ) { |
| 167 |
$text = esc_html__( 'Switch to Infinite plan for unlimited optimization:', 'imagify' ) . '<br>'; |
| 168 |
|
| 169 |
if ( $this->user->is_monthly ) { |
| 170 |
$text .= esc_html__( 'For $11.99/month, optimize as many images as you like!', 'imagify' ); |
| 171 |
$upgrade_link = IMAGIFY_APP_DOMAIN . '/subscription/plan_switch/?label=infinite&payment_plan=1&utm_source=plugin&utm_medium=notification '; |
| 172 |
} else { |
| 173 |
$text .= esc_html__( 'For $9.99/month, optimize as many images as you like!', 'imagify' ); |
| 174 |
$upgrade_link = IMAGIFY_APP_DOMAIN . '/subscription/plan_switch/?label=infinite&payment_plan=2&utm_source=plugin&utm_medium=notification '; |
| 175 |
} |
| 176 |
|
| 177 |
$button_text = esc_html__( 'Switch To Infinite Plan', 'imagify' ); |
| 178 |
} |
| 179 |
|
| 180 |
$data = [ |
| 181 |
'quota_icon' => $views->get_quota_icon(), |
| 182 |
'quota_class' => $views->get_quota_class(), |
| 183 |
'plan_label' => $this->user->plan_label, |
| 184 |
'plan_with_quota' => $this->user->is_free() || $this->user->is_growth(), |
| 185 |
'unconsumed_quota' => $unconsumed_quota, |
| 186 |
'user_quota' => $this->user->get_quota(), |
| 187 |
'next_update' => $this->user->next_date_update, |
| 188 |
'text' => $text, |
| 189 |
'button_text' => $button_text, |
| 190 |
'upgrade_link' => $upgrade_link, |
| 191 |
]; |
| 192 |
|
| 193 |
$template = [ |
| 194 |
'admin_bar_status' => $views->get_template( 'admin/admin-bar-status', $data ), |
| 195 |
'admin_bar_pricing' => $views->get_template( |
| 196 |
'admin/admin-bar-pricing', |
| 197 |
[ |
| 198 |
'upgrade_pricing' => $this->user->is_free() && ( $this->user->get_percent_unconsumed_quota() > 20 ), |
| 199 |
] |
| 200 |
), |
| 201 |
]; |
| 202 |
|
| 203 |
wp_send_json_success( $template ); |
| 204 |
} |
| 205 |
} |
| 206 |
|