| 1 |
<?php // phpcs:ignore Squiz.Commenting.FileComment.Missing -- File doc comment exists |
| 2 |
/** |
| 3 |
* Analytify Email Bootstrap Trait |
| 4 |
* |
| 5 |
* This trait contains the core initialization, constructor, hooks, and constants |
| 6 |
* for the Analytify Email functionality. It was created to separate the bootstrap |
| 7 |
* logic from other email operations, keeping the code organized and maintainable. |
| 8 |
* |
| 9 |
* PURPOSE: |
| 10 |
* - Handles class initialization and constructor logic |
| 11 |
* - Sets up WordPress hooks and actions |
| 12 |
* - Manages plugin constants and core setup |
| 13 |
* - Provides backward compatibility checks |
| 14 |
* |
| 15 |
* @package WP_Analytify |
| 16 |
* @subpackage Email |
| 17 |
* @since 8.0.0 |
| 18 |
*/ |
| 19 |
|
| 20 |
trait Analytify_Email_Bootstrap { |
| 21 |
|
| 22 |
/** |
| 23 |
* Main Analytify object. |
| 24 |
* |
| 25 |
* @var mixed |
| 26 |
*/ |
| 27 |
private $WP_ANALYTIFY = ''; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.PropertyNotSnakeCase -- Variable name is acceptable for this context |
| 28 |
|
| 29 |
/** |
| 30 |
* Constructor - Initialize the email functionality. |
| 31 |
* |
| 32 |
* @since 1.0.0 |
| 33 |
*/ |
| 34 |
public function __construct() { |
| 35 |
if ( ! $this->verify_update() ) { |
| 36 |
return; |
| 37 |
} |
| 38 |
|
| 39 |
$this->WP_ANALYTIFY = $GLOBALS['WP_ANALYTIFY']; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- Variable name is acceptable for this context |
| 40 |
|
| 41 |
$this->setup_constants(); |
| 42 |
$this->anaytify_email_check_time(); |
| 43 |
$this->hooks(); |
| 44 |
|
| 45 |
if ( isset( $_POST['test_email'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Nonce verification is handled in the callback function |
| 46 |
$this->callback_on_cron_time(); |
| 47 |
add_action( 'admin_notices', array( $this, 'analytify_email_notics' ) ); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Setup WordPress hooks and actions. |
| 53 |
* |
| 54 |
* @return void |
| 55 |
* @since 1.0.0 |
| 56 |
*/ |
| 57 |
public function hooks() { |
| 58 |
add_action( 'admin_enqueue_scripts', array( $this, 'analytify_email_scripts' ) ); |
| 59 |
add_action( 'analytify_email_cron_function', array( $this, 'callback_on_cron_time' ) ); |
| 60 |
add_filter( 'wp_analytify_pro_setting_tabs', array( $this, 'analytify_email_setting_tabs' ), 20, 1 ); |
| 61 |
add_filter( 'wp_analytify_pro_setting_fields', array( $this, 'analytifye_email_setting_fields' ), 20, 1 ); |
| 62 |
add_action( 'after_single_view_stats_buttons', array( $this, 'single_send_email' ) ); |
| 63 |
add_action( 'wp_ajax_send_analytics_email', array( $this, 'send_analytics_email' ) ); |
| 64 |
add_action( 'wp_ajax_nopriv_analytify_send_single_email_background', array( $this, 'send_analytics_email_background' ) ); |
| 65 |
add_action( 'analytify_settings_logs', array( $this, 'analytify_settings_logs' ) ); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Enqueue scripts for email functionality. |
| 70 |
* |
| 71 |
* @return void |
| 72 |
* @since 1.0 |
| 73 |
*/ |
| 74 |
public function analytify_email_scripts() { |
| 75 |
wp_enqueue_script( 'analytify_email_script', ( defined( 'ANALYTIFY_PLUGIN_URL' ) ? ANALYTIFY_PLUGIN_URL : '' ) . 'assets/js/wp-analytify-email' . analytify_get_asset_suffix() . '.js', array( 'jquery' ), defined( 'ANALYTIFY_VERSION' ) ? ANALYTIFY_VERSION : '1.0.0', true ); |
| 76 |
|
| 77 |
// Localize nonce data for email script. |
| 78 |
// Get existing nonces if wpanalytify_data was already localized to wp-analytify-script-js. |
| 79 |
$nonces = apply_filters( |
| 80 |
'wpanalytify_nonces', |
| 81 |
array( |
| 82 |
'send_single_post_email' => wp_create_nonce( 'analytify-single-post-email' ), |
| 83 |
'activate_license' => wp_create_nonce( 'activate-license' ), |
| 84 |
) |
| 85 |
); |
| 86 |
|
| 87 |
// Localize wpanalytify_data to email script to ensure nonces are available. |
| 88 |
// This will merge with existing wpanalytify_data if it was already defined by wp-analytify-script-js. |
| 89 |
wp_localize_script( |
| 90 |
'analytify_email_script', |
| 91 |
'wpanalytify_data', |
| 92 |
array( |
| 93 |
'nonces' => $nonces, |
| 94 |
) |
| 95 |
); |
| 96 |
|
| 97 |
// Localized strings for email UI (translation / _x). Always localized when this script is enqueued. |
| 98 |
wp_localize_script( |
| 99 |
'analytify_email_script', |
| 100 |
'wpAnalytifyEmail', |
| 101 |
array( |
| 102 |
'sendEmailReport' => _x( 'Send Email Report', 'button label', 'wp-analytify' ), |
| 103 |
'sending' => _x( 'Sending...', 'button state while sending', 'wp-analytify' ), |
| 104 |
'placeholderRecipient' => _x( 'Enter recipient email', 'placeholder for recipient email input', 'wp-analytify' ), |
| 105 |
'emailReportSent' => _x( 'Email Report Sent!', 'success message after sending single post email', 'wp-analytify' ), |
| 106 |
'emailSendFailed' => _x( 'Failed to send email. Please try again.', 'error when single post email request fails', 'wp-analytify' ), |
| 107 |
) |
| 108 |
); |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Email submenu placeholder function. |
| 113 |
* |
| 114 |
* @return void |
| 115 |
* @since 1.0.0 |
| 116 |
*/ |
| 117 |
public function analytify_email_setting() { |
| 118 |
// Placeholder for email settings. |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Setup plugin constants. |
| 123 |
* |
| 124 |
* @access private |
| 125 |
* @since 1.0.0 |
| 126 |
* @return void |
| 127 |
*/ |
| 128 |
private function setup_constants() { |
| 129 |
// Setting Global Values. |
| 130 |
$this->define( 'ANALYTIFY_IMAGES_PATH', 'https://analytify.io/assets/email/' ); |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Define constant if not already set. |
| 135 |
* |
| 136 |
* @param string $name Constant name. |
| 137 |
* @param mixed $value Constant value. |
| 138 |
* @return void |
| 139 |
*/ |
| 140 |
private function define( $name, $value ) { |
| 141 |
if ( ! defined( $name ) ) { |
| 142 |
define( $name, $value ); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Check and schedule email cron events. |
| 148 |
* |
| 149 |
* @return void |
| 150 |
* @since 1.0.0 |
| 151 |
*/ |
| 152 |
public function anaytify_email_check_time() { |
| 153 |
// Check if event is scheduled before. |
| 154 |
if ( ! wp_next_scheduled( 'analytify_email_cron_function' ) ) { |
| 155 |
wp_schedule_event( time(), 'daily', 'analytify_email_cron_function' ); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Verify email addon. |
| 161 |
* |
| 162 |
* Check if email addon is already present and is prior to latest split functionality version. |
| 163 |
* |
| 164 |
* @return bool |
| 165 |
*/ |
| 166 |
public function verify_update() { |
| 167 |
if ( defined( 'ANALTYIFY_EMAIL_VERSION' ) && '1.2.8' >= ANALTYIFY_EMAIL_VERSION ) { |
| 168 |
return false; |
| 169 |
} |
| 170 |
|
| 171 |
return true; |
| 172 |
} |
| 173 |
} |
| 174 |
|