| 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 |
'wp_ajax_imagify_get_admin_bar_profile' => 'get_admin_bar_profile_callback', |
| 39 |
'admin_bar_menu' => [ 'add_imagify_admin_bar_menu', IMAGIFY_INT_MAX ], |
| 40 |
]; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Add Imagify menu in the admin bar. |
| 45 |
* |
| 46 |
* @param WP_Admin_Bar $wp_admin_bar WP_Admin_Bar instance, passed by reference. |
| 47 |
*/ |
| 48 |
public function add_imagify_admin_bar_menu( $wp_admin_bar ) { |
| 49 |
if ( ! imagify_get_context( 'wp' )->current_user_can( 'manage' ) ) { |
| 50 |
return; |
| 51 |
} |
| 52 |
|
| 53 |
if ( ! get_imagify_option( 'admin_bar_menu' ) ) { |
| 54 |
return; |
| 55 |
} |
| 56 |
|
| 57 |
// Parent. |
| 58 |
$wp_admin_bar->add_menu( array( |
| 59 |
'id' => 'imagify', |
| 60 |
'title' => 'Imagify', |
| 61 |
'href' => get_imagify_admin_url(), |
| 62 |
) ); |
| 63 |
|
| 64 |
// Settings. |
| 65 |
$wp_admin_bar->add_menu(array( |
| 66 |
'parent' => 'imagify', |
| 67 |
'id' => 'imagify-settings', |
| 68 |
'title' => __( 'Settings' ), |
| 69 |
'href' => get_imagify_admin_url(), |
| 70 |
) ); |
| 71 |
|
| 72 |
// Bulk Optimization. |
| 73 |
if ( ! is_network_admin() ) { |
| 74 |
$wp_admin_bar->add_menu(array( |
| 75 |
'parent' => 'imagify', |
| 76 |
'id' => 'imagify-bulk-optimization', |
| 77 |
'title' => __( 'Bulk Optimization', 'imagify' ), |
| 78 |
'href' => get_imagify_admin_url( 'bulk-optimization' ), |
| 79 |
) ); |
| 80 |
} |
| 81 |
|
| 82 |
// Documentation. |
| 83 |
$wp_admin_bar->add_menu(array( |
| 84 |
'parent' => 'imagify', |
| 85 |
'id' => 'imagify-documentation', |
| 86 |
'title' => __( 'Documentation', 'imagify' ), |
| 87 |
'href' => imagify_get_external_url( 'documentation' ), |
| 88 |
'meta' => array( |
| 89 |
'target' => '_blank', |
| 90 |
), |
| 91 |
) ); |
| 92 |
|
| 93 |
// Rate it. |
| 94 |
$wp_admin_bar->add_menu(array( |
| 95 |
'parent' => 'imagify', |
| 96 |
'id' => 'imagify-rate-it', |
| 97 |
/* translators: %s is WordPress.org. */ |
| 98 |
'title' => sprintf( __( 'Rate Imagify on %s', 'imagify' ), 'WordPress.org' ), |
| 99 |
'href' => imagify_get_external_url( 'rate' ), |
| 100 |
'meta' => array( |
| 101 |
'target' => '_blank', |
| 102 |
), |
| 103 |
) ); |
| 104 |
|
| 105 |
// Quota & Profile informations. |
| 106 |
if ( defined( 'IMAGIFY_HIDDEN_ACCOUNT' ) && IMAGIFY_HIDDEN_ACCOUNT || ! get_imagify_option( 'api_key' ) ) { |
| 107 |
return; |
| 108 |
} |
| 109 |
|
| 110 |
if ( |
| 111 |
$this->user->is_free() |
| 112 |
&& |
| 113 |
$this->user->get_percent_unconsumed_quota() > 20 |
| 114 |
) { |
| 115 |
$wp_admin_bar->add_menu( [ |
| 116 |
'parent' => 'imagify', |
| 117 |
'id' => 'imagify-upgrade-plan', |
| 118 |
'title' => '<button data-nonce="' . wp_create_nonce( 'imagify_get_pricing_' . get_current_user_id() ) . '" data-target="#imagify-pricing-modal" type="button" class="imagify-get-pricing-modal imagify-modal-trigger imagify-admin-bar-upgrade-plan">' . __( 'Upgrade Plan', 'imagify' ) . '</button>', |
| 119 |
] ); |
| 120 |
} |
| 121 |
|
| 122 |
$wp_admin_bar->add_menu( array( |
| 123 |
'parent' => 'imagify', |
| 124 |
'id' => 'imagify-profile', |
| 125 |
'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>', |
| 126 |
) ); |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Get admin bar profile output. |
| 131 |
* |
| 132 |
* @return void |
| 133 |
*/ |
| 134 |
public function get_admin_bar_profile_callback() { |
| 135 |
imagify_check_nonce( 'imagify-get-admin-bar-profile', 'imagifygetadminbarprofilenonce' ); |
| 136 |
|
| 137 |
if ( ! imagify_get_context( 'wp' )->current_user_can( 'manage' ) ) { |
| 138 |
imagify_die(); |
| 139 |
} |
| 140 |
|
| 141 |
$views = Imagify_Views::get_instance(); |
| 142 |
$unconsumed_quota = $views->get_quota_percent(); |
| 143 |
$text = ''; |
| 144 |
$button_text = ''; |
| 145 |
$upgrade_link = ''; |
| 146 |
|
| 147 |
if ( $this->user->is_free() ) { |
| 148 |
$text = esc_html__( 'Upgrade your plan now for more!', 'rocket' ) . '<br>' . |
| 149 |
esc_html__( 'From $5.99/month only, keep going with image optimization!', 'rocket' ); |
| 150 |
$button_text = esc_html__( 'Upgrade My Plan', 'rocket' ); |
| 151 |
$upgrade_link = IMAGIFY_APP_DOMAIN . '/subscription/?utm_source=plugin&utm_medium=notification'; |
| 152 |
} elseif ( $this->user->is_growth() ) { |
| 153 |
$text = esc_html__( 'Switch to Infinite plan for unlimited optimization:', 'rocket' ) . '<br>'; |
| 154 |
|
| 155 |
if ( $this->user->is_monthly ) { |
| 156 |
$text .= esc_html__( 'For $9.99/month, optimize as many images as you like!', 'rocket' ); |
| 157 |
$upgrade_link = IMAGIFY_APP_DOMAIN . '/subscription/plan_switch/?label=infinite&payment_plan=1&utm_source=plugin&utm_medium=notification '; |
| 158 |
} else { |
| 159 |
$text .= esc_html__( 'For $99.9/year, optimize as many images as you like!', 'rocket' ); |
| 160 |
$upgrade_link = IMAGIFY_APP_DOMAIN . '/subscription/plan_switch/?label=infinite&payment_plan=2&utm_source=plugin&utm_medium=notification '; |
| 161 |
} |
| 162 |
|
| 163 |
$button_text = esc_html__( 'Switch To Infinite Plan', 'rocket' ); |
| 164 |
} |
| 165 |
|
| 166 |
$data = [ |
| 167 |
'quota_icon' => $views->get_quota_icon(), |
| 168 |
'quota_class' => $views->get_quota_class(), |
| 169 |
'plan_label' => $this->user->plan_label, |
| 170 |
'plan_with_quota' => $this->user->is_free() || $this->user->is_growth(), |
| 171 |
'unconsumed_quota' => $unconsumed_quota, |
| 172 |
'user_quota' => $this->user->quota, |
| 173 |
'next_update' => $this->user->next_date_update, |
| 174 |
'text' => $text, |
| 175 |
'button_text' => $button_text, |
| 176 |
'upgrade_link' => $upgrade_link, |
| 177 |
]; |
| 178 |
|
| 179 |
$template = $views->get_template( 'admin/admin-bar-status', $data ); |
| 180 |
|
| 181 |
wp_send_json_success( $template ); |
| 182 |
} |
| 183 |
} |
| 184 |
|