| 1 |
<?php |
| 2 |
|
| 3 |
namespace YayMail\Notices; |
| 4 |
|
| 5 |
use YayMail\Utils\SingletonTrait; |
| 6 |
use YayMail\Notices\Ajax; |
| 7 |
use YayMail\SupportedPlugins; |
| 8 |
use YayMail\Utils\Logger; |
| 9 |
|
| 10 |
/** |
| 11 |
* |
| 12 |
* @method static NoticeMain get_instance() |
| 13 |
*/ |
| 14 |
class NoticeMain { |
| 15 |
use SingletonTrait; |
| 16 |
|
| 17 |
private $logger; |
| 18 |
|
| 19 |
private $supported_plugins; |
| 20 |
|
| 21 |
protected function __construct() { |
| 22 |
$this->logger = new Logger(); |
| 23 |
|
| 24 |
Ajax::get_instance(); |
| 25 |
|
| 26 |
$this->supported_plugins = SupportedPlugins::get_instance(); |
| 27 |
|
| 28 |
$this->init_hooks(); |
| 29 |
} |
| 30 |
|
| 31 |
protected function init_hooks() { |
| 32 |
if ( ! function_exists( 'YayMail\init' ) ) { |
| 33 |
return; |
| 34 |
} |
| 35 |
// Show recommendation notice |
| 36 |
if ( time() >= (int) get_option( 'yaymail_next_recommendation_suggest_addons_notice_time' ) ) { |
| 37 |
add_action( 'admin_notices', [ $this, 'render_suggest_addons_notice' ] ); |
| 38 |
} |
| 39 |
|
| 40 |
if ( time() >= (int) get_option( 'yaymail_next_recommendation_upgrade_notice_time' ) ) { |
| 41 |
add_action( 'admin_notices', [ $this, 'render_upgrade_notice' ] ); |
| 42 |
} |
| 43 |
|
| 44 |
if ( is_admin() ) { |
| 45 |
if ( time() >= (int) get_option( 'yaymail_next_recommendation_notice_time' ) || time() >= (int) get_option( 'yaymail_next_recommendation_suggest_addons_notice_time' ) || time() >= (int) get_option( 'yaymail_next_recommendation_upgrade_notice_time' ) ) { |
| 46 |
wp_enqueue_script( 'yaymail-notice', YAYMAIL_PLUGIN_URL . 'assets/scripts/notice.js', [ 'jquery' ], yaymail_version(), false ); |
| 47 |
} |
| 48 |
|
| 49 |
wp_localize_script( |
| 50 |
'yaymail-notice', |
| 51 |
'yaymail_notice', |
| 52 |
[ |
| 53 |
'admin_ajax' => admin_url( 'admin-ajax.php' ), |
| 54 |
'nonce' => wp_create_nonce( 'yaymail_nonce' ), |
| 55 |
] |
| 56 |
); |
| 57 |
} |
| 58 |
|
| 59 |
add_action( |
| 60 |
'after_plugin_row_' . YAYMAIL_PLUGIN_BASENAME, |
| 61 |
[ $this, 'display_under_plugin_notices' ], |
| 62 |
10, |
| 63 |
2 |
| 64 |
); |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public function display_under_plugin_notices( $plugin_file ) { |
| 70 |
|
| 71 |
$plugins = get_plugins(); |
| 72 |
|
| 73 |
$yaymail_addons = array_filter( |
| 74 |
$plugins, |
| 75 |
function ( $key ) use ( $plugins ) { |
| 76 |
if ( ! is_string( $key ) ) { |
| 77 |
return false; |
| 78 |
} |
| 79 |
$plugin_data = $plugins[ $key ]; |
| 80 |
$is_yaycommerce_author = isset( $plugin_data['Author'] ) && strpos( $plugin_data['Author'], 'YayCommerce' ) !== false; |
| 81 |
|
| 82 |
return $is_yaycommerce_author && ( |
| 83 |
strpos( $key, 'yaymail-addon' ) !== false || |
| 84 |
strpos( $key, 'email-customizer' ) !== false || |
| 85 |
strpos( $key, 'yaymail-premium-addon' ) !== false || |
| 86 |
strpos( $key, 'yaymail-conditional-logic' ) !== false |
| 87 |
); |
| 88 |
}, |
| 89 |
ARRAY_FILTER_USE_KEY |
| 90 |
); |
| 91 |
|
| 92 |
$addon_info = []; |
| 93 |
foreach ( $yaymail_addons as $plugin_path => $plugin_data ) { |
| 94 |
if ( is_plugin_active( $plugin_path ) && version_compare( $plugin_data['Version'], '4.0', '<' ) ) { |
| 95 |
$addon_info[] = [ |
| 96 |
'name' => $plugin_data['Name'], |
| 97 |
'version' => $plugin_data['Version'], |
| 98 |
]; |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
if ( empty( $addon_info ) ) { |
| 103 |
return; |
| 104 |
} |
| 105 |
|
| 106 |
$wp_list_table = _get_list_table( 'WP_MS_Themes_List_Table' ); |
| 107 |
|
| 108 |
add_action( 'admin_footer', [ $this, 'enqueue_admin_script' ] ); |
| 109 |
|
| 110 |
echo wp_kses_post( |
| 111 |
'<tr class="plugin-update-tr' . ( is_plugin_active( $plugin_file ) ? ' active' : '' ) . '"> |
| 112 |
<td colspan="' . esc_attr( $wp_list_table->get_column_count() ) . '" class="plugin-update colspanchange">' |
| 113 |
. $this->get_addon_update_notice() |
| 114 |
. '</td> |
| 115 |
</tr>' |
| 116 |
); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Enqueues a script to modify the plugin row styling in the admin footer. |
| 121 |
*/ |
| 122 |
public function enqueue_admin_script() { |
| 123 |
?> |
| 124 |
<script> |
| 125 |
document.addEventListener('DOMContentLoaded', function() { |
| 126 |
var pluginRow = document.querySelector('tr[data-plugin="<?php echo esc_js( YAYMAIL_PLUGIN_BASENAME ); ?>"]'); |
| 127 |
if (pluginRow) pluginRow.classList.add('update'); |
| 128 |
}); |
| 129 |
</script> |
| 130 |
<?php |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Returns the notice to update to new core version. |
| 135 |
*/ |
| 136 |
protected function get_addon_update_notice() { |
| 137 |
return sprintf( |
| 138 |
'<div class="notice inline notice-error notice-alt"><p>%s</p></div>', |
| 139 |
esc_html__( 'Please update the YayMail Addons to version 4.0 or later to ensure compatibility and optimal performance.', 'yaymail' ), |
| 140 |
); |
| 141 |
} |
| 142 |
|
| 143 |
public function render_suggest_addons_notice() { |
| 144 |
/* List out the 3rd-party that need our addon*/ |
| 145 |
$addon_needed_plugins = []; |
| 146 |
foreach ( $this->supported_plugins->get_addon_supported_plugins() as $addon_namespace => $addon ) { |
| 147 |
if ( ! empty( $addon['is_3rd_party_installed'] ) && empty( $addon['installation_status']['is_active'] ) ) { |
| 148 |
$addon_needed_plugins[ $addon_namespace ] = $addon; |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
if ( ! empty( $addon_needed_plugins ) ) { |
| 153 |
include YAYMAIL_PLUGIN_PATH . 'templates/notices/suggest-addons.php'; |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
public function render_upgrade_notice() { |
| 158 |
|
| 159 |
$pro_needed_plugins = $this->supported_plugins->get_pro_supported_plugins(); |
| 160 |
|
| 161 |
if ( ! empty( $pro_needed_plugins ) ) { |
| 162 |
include YAYMAIL_PLUGIN_PATH . 'templates/notices/upgrade.php'; |
| 163 |
} |
| 164 |
} |
| 165 |
} |
| 166 |
|