| 1 |
<?php |
| 2 |
|
| 3 |
namespace YayMail\Notices; |
| 4 |
|
| 5 |
use YayMail\Utils\SingletonTrait; |
| 6 |
|
| 7 |
defined( 'ABSPATH' ) || exit; |
| 8 |
|
| 9 |
/** |
| 10 |
* NoticeWPMail Class |
| 11 |
* |
| 12 |
* @method static NoticeWPMail get_instance() |
| 13 |
*/ |
| 14 |
class NoticeWPMail { |
| 15 |
use SingletonTrait; |
| 16 |
|
| 17 |
protected function __construct() { |
| 18 |
add_action( |
| 19 |
'after_plugin_row_' . YAYMAIL_PLUGIN_BASENAME, |
| 20 |
[ $this, 'display_under_plugin_notices' ], |
| 21 |
100, |
| 22 |
2 |
| 23 |
); |
| 24 |
|
| 25 |
add_action( 'admin_footer', [ $this, 'enqueue_admin_script' ] ); |
| 26 |
|
| 27 |
// Handle Yay WP installation |
| 28 |
add_action( 'admin_action_yay_wp_install', [ $this, 'install_yay_wp' ] ); |
| 29 |
|
| 30 |
// Show admin notices after installation |
| 31 |
add_action( 'admin_notices', [ $this, 'show_install_notices' ] ); |
| 32 |
} |
| 33 |
|
| 34 |
private function is_yay_wp_installed() { |
| 35 |
return defined( 'YAYMAIL_WP_VERSION' ); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Displays the required notices below the plugin row if dependencies are missing. |
| 40 |
* |
| 41 |
* @param string $plugin_file |
| 42 |
*/ |
| 43 |
public function display_under_plugin_notices( $plugin_file ) { |
| 44 |
if ( $this->is_yay_wp_installed() ) { |
| 45 |
return; |
| 46 |
// No need to show notices if dependencies are met |
| 47 |
} |
| 48 |
|
| 49 |
$wp_list_table = _get_list_table( 'WP_MS_Themes_List_Table' ); |
| 50 |
|
| 51 |
echo wp_kses_post( |
| 52 |
'<tr class="plugin-update-tr' . ( is_plugin_active( $plugin_file ) ? ' active' : '' ) . '"> |
| 53 |
<td colspan="' . esc_attr( $wp_list_table->get_column_count() ) . '" class="plugin-update colspanchange">' |
| 54 |
. ( ! $this->is_yay_wp_installed() ? $this->get_yay_wp_required_notice() : '' ) |
| 55 |
. '</td> |
| 56 |
</tr>' |
| 57 |
); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Returns the notice for missing YayMail - WP Email Customizer plugin. |
| 62 |
*/ |
| 63 |
protected function get_yay_wp_required_notice() { |
| 64 |
$yay_wp_versions = [ |
| 65 |
'yay-wp-email-customizer/yay-wp-email-customizer.php', |
| 66 |
'yay-wp-email-customizer-pro/yay-wp-email-customizer.php', |
| 67 |
]; |
| 68 |
|
| 69 |
$all_plugins = get_plugins(); |
| 70 |
$plugin_to_activate = null; |
| 71 |
|
| 72 |
foreach ( $yay_wp_versions as $plugin_file ) { |
| 73 |
if ( array_key_exists( $plugin_file, $all_plugins ) && ! is_plugin_active( $plugin_file ) ) { |
| 74 |
$plugin_to_activate = $plugin_file; |
| 75 |
break; |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
if ( $plugin_to_activate ) { |
| 80 |
$activate_url = wp_nonce_url( |
| 81 |
admin_url( 'plugins.php?action=activate&plugin=' . urlencode( $plugin_to_activate ) ), |
| 82 |
'activate-plugin_' . $plugin_to_activate |
| 83 |
); |
| 84 |
|
| 85 |
return sprintf( |
| 86 |
'<div class="notice inline notice-warning notice-alt"><p>%s <a href="%s">%s</a></p></div>', |
| 87 |
esc_html__( 'To customize WordPress core emails, you need to activate Yay WP Email Customizer plugin.', 'yaymail' ), |
| 88 |
esc_url( $activate_url ), |
| 89 |
esc_html__( 'Activate Now', 'yaymail' ) |
| 90 |
); |
| 91 |
} |
| 92 |
|
| 93 |
$install_url = wp_nonce_url( |
| 94 |
admin_url( 'admin.php?action=yay_wp_install' ), |
| 95 |
'yay-wp-install' |
| 96 |
); |
| 97 |
|
| 98 |
return sprintf( |
| 99 |
'<div class="notice inline notice-warning notice-alt"><p>%s <a href="%s">%s</a></p></div>', |
| 100 |
esc_html__( 'To customize WordPress core emails, you need to install and activate Yay WP Email Customizer plugin. Get', 'yaymail' ), |
| 101 |
esc_url( $install_url ), |
| 102 |
esc_html__( 'Yay WP Email Customizer', 'yaymail' ), |
| 103 |
); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Handles the installation of YayMail - WP Email Customizer plugin. |
| 108 |
*/ |
| 109 |
public function install_yaymail_wp() { |
| 110 |
// Check user permissions |
| 111 |
if ( ! current_user_can( 'install_plugins' ) ) { |
| 112 |
wp_die( esc_html__( 'You do not have permission to install plugins.', 'yaymail' ) ); |
| 113 |
} |
| 114 |
|
| 115 |
// Verify nonce |
| 116 |
check_admin_referer( 'yay-wp-install' ); |
| 117 |
|
| 118 |
// Include required WordPress files |
| 119 |
require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 120 |
require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; |
| 121 |
require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 122 |
require_once ABSPATH . 'wp-admin/includes/misc.php'; |
| 123 |
|
| 124 |
// Create upgrader instance |
| 125 |
$upgrader = new \Plugin_Upgrader( new \WP_Ajax_Upgrader_Skin() ); |
| 126 |
|
| 127 |
// Install the plugin |
| 128 |
$download_url = 'https://downloads.wordpress.org/plugin/yay-wp-email-customizer.zip'; |
| 129 |
$result = $upgrader->install( $download_url ); |
| 130 |
|
| 131 |
// Check if installation was successful |
| 132 |
if ( is_wp_error( $result ) ) { |
| 133 |
wp_redirect( admin_url( 'plugins.php?yay-wp-install-error=1' ) ); |
| 134 |
exit; |
| 135 |
} |
| 136 |
|
| 137 |
// Try to activate the plugin |
| 138 |
$plugin_file = 'yay-wp-email-customizer/yay-wp-email-customizer.php'; |
| 139 |
$activate_result = activate_plugin( $plugin_file ); |
| 140 |
|
| 141 |
if ( is_wp_error( $activate_result ) ) { |
| 142 |
wp_redirect( admin_url( 'plugins.php?yay-wp-installed=1&yay-wp-activate-error=1' ) ); |
| 143 |
exit; |
| 144 |
} |
| 145 |
|
| 146 |
// Success - redirect back to plugins page |
| 147 |
wp_redirect( admin_url( 'plugins.php?yay-wp-installed=1&yay-wp-activated=1' ) ); |
| 148 |
exit; |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Displays admin notices after plugin installation. |
| 153 |
*/ |
| 154 |
public function show_install_notices() { |
| 155 |
if ( isset( $_GET['yay-wp-install-error'] ) ) { |
| 156 |
?> |
| 157 |
<div class="notice notice-error is-dismissible"> |
| 158 |
<p><?php esc_html_e( 'Failed to install Yay WP Email Customizer plugin. Please try installing it manually from WordPress.org.', 'yaymail' ); ?></p> |
| 159 |
</div> |
| 160 |
<?php |
| 161 |
} |
| 162 |
|
| 163 |
if ( isset( $_GET['yay-wp-installed'] ) && isset( $_GET['yay-wp-activated'] ) ) { |
| 164 |
?> |
| 165 |
<div class="notice notice-success is-dismissible"> |
| 166 |
<p><?php esc_html_e( 'Yay WP Email Customizer plugin has been successfully installed and activated!', 'yaymail' ); ?></p> |
| 167 |
</div> |
| 168 |
<?php |
| 169 |
} elseif ( isset( $_GET['yay-wp-installed'] ) && isset( $_GET['yay-wp-activate-error'] ) ) { |
| 170 |
?> |
| 171 |
<div class="notice notice-warning is-dismissible"> |
| 172 |
<p><?php esc_html_e( 'Yay WP Email Customizer plugin was installed but could not be activated automatically. Please activate it manually.', 'yaymail' ); ?></p> |
| 173 |
</div> |
| 174 |
<?php |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Enqueues a script to modify the plugin row styling in the admin footer. |
| 180 |
*/ |
| 181 |
public function enqueue_admin_script() { |
| 182 |
// Check is Yay WP Email Customizer plugin installed |
| 183 |
if ( $this->is_yay_wp_installed() ) { |
| 184 |
return; |
| 185 |
} |
| 186 |
?> |
| 187 |
<script> |
| 188 |
document.addEventListener('DOMContentLoaded', function() { |
| 189 |
var pluginRow = document.querySelector('tr[data-plugin="<?php echo esc_js( YAYMAIL_PLUGIN_BASENAME ); ?>"]'); |
| 190 |
if (pluginRow) pluginRow.classList.add('update'); |
| 191 |
}); |
| 192 |
</script> |
| 193 |
<?php |
| 194 |
} |
| 195 |
} |
| 196 |
|