| 1 |
<?php // phpcs:ignore Squiz.Commenting.FileComment.Missing -- File doc comment exists |
| 2 |
/** |
| 3 |
* Analytify Email Settings Trait |
| 4 |
* |
| 5 |
* This trait contains all the settings-related functionality for the Analytify Email system. |
| 6 |
* It was created to separate settings configuration from other email operations, keeping |
| 7 |
* the code organized and maintainable. |
| 8 |
* |
| 9 |
* PURPOSE: |
| 10 |
* - Manages email settings tabs and fields |
| 11 |
* - Handles settings configuration and options |
| 12 |
* - Provides settings validation and sanitization |
| 13 |
* - Manages email settings display and form handling |
| 14 |
* |
| 15 |
* @package WP_Analytify |
| 16 |
* @subpackage Email |
| 17 |
* @since 8.0.0 |
| 18 |
*/ |
| 19 |
|
| 20 |
trait Analytify_Email_Settings { |
| 21 |
|
| 22 |
/** |
| 23 |
* Add email settings tabs to the main settings page. |
| 24 |
* |
| 25 |
* @param array<string, mixed> $old_tabs Existing tabs array. |
| 26 |
* @return array<string, mixed> Modified tabs array. |
| 27 |
* @since 1.0.0 |
| 28 |
*/ |
| 29 |
public function analytify_email_setting_tabs( $old_tabs ) { |
| 30 |
$pro_tabs = array( |
| 31 |
'email_tab' => array( |
| 32 |
'id' => 'wp-analytify-email', |
| 33 |
'title' => __( 'Email', 'wp-analytify' ), |
| 34 |
'priority' => '32', |
| 35 |
), |
| 36 |
); |
| 37 |
|
| 38 |
return array_merge( $old_tabs, $pro_tabs ); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Add email settings fields to the main settings page. |
| 43 |
* |
| 44 |
* @param array<string, mixed> $old_fields Existing fields array. |
| 45 |
* @return array<string, mixed> Modified fields array. |
| 46 |
* @since 1.0.0 |
| 47 |
*/ |
| 48 |
public function analytifye_email_setting_fields( $old_fields ) { |
| 49 |
$email_fields = array( |
| 50 |
'wp-analytify-email' => array( |
| 51 |
array( |
| 52 |
'name' => 'disable_email_reports', |
| 53 |
'label' => __( 'Disable Email Reporting', 'wp-analytify' ), |
| 54 |
'desc' => __( 'This option will stop sending all email reports, including test emails.', 'wp-analytify' ), |
| 55 |
'type' => 'checkbox', |
| 56 |
), |
| 57 |
array( |
| 58 |
'name' => 'analytiy_from_email', |
| 59 |
'label' => __( 'Sender Email Address', 'wp-analytify' ), |
| 60 |
'desc' => __( 'Sender Email Address.', 'wp-analytify' ), |
| 61 |
'type' => 'text', |
| 62 |
'default' => '', |
| 63 |
'sanitize_callback' => 'sanitize_email', |
| 64 |
'tooltip' => false, |
| 65 |
), |
| 66 |
array( |
| 67 |
'name' => 'analytify_email_user_email', |
| 68 |
'label' => __( 'Receiver Email Address', 'wp-analytify' ), |
| 69 |
'desc' => '', |
| 70 |
'default' => '', |
| 71 |
'type' => 'email_receivers', |
| 72 |
), |
| 73 |
), |
| 74 |
); |
| 75 |
|
| 76 |
if ( ! class_exists( 'WP_Analytify_Email' ) && ! class_exists( 'WP_Analytify_Addon_Email' ) ) { |
| 77 |
array_push( |
| 78 |
$email_fields['wp-analytify-email'], |
| 79 |
array( |
| 80 |
'name' => 'analytify_email_promo', |
| 81 |
'type' => 'email_promo', |
| 82 |
'label' => '', |
| 83 |
'desc' => '', |
| 84 |
) |
| 85 |
); |
| 86 |
} |
| 87 |
|
| 88 |
return array_merge( $old_fields, $email_fields ); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Display email settings notices. |
| 93 |
* |
| 94 |
* @return void |
| 95 |
* @since 1.0.0 |
| 96 |
*/ |
| 97 |
public function analytify_email_notics() { |
| 98 |
$email_options = get_option( 'wp-analytify-email' ); |
| 99 |
|
| 100 |
if ( isset( $email_options['disable_email_reports'] ) && 'on' === $email_options['disable_email_reports'] ) { |
| 101 |
$class = 'wp-analytify-danger'; |
| 102 |
$message = esc_html( 'Analytify email reports and test emails disabled.' ); |
| 103 |
} else { |
| 104 |
$class = 'wp-analytify-success wp-analytify-email-report-sent'; |
| 105 |
$message = esc_html__( 'Analytify report sent.', 'wp-analytify' ); |
| 106 |
} |
| 107 |
|
| 108 |
analytify_notice( $message, $class ); |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Custom PHPMailer initialization for SMTP. |
| 113 |
* |
| 114 |
* @param mixed $PHPMailer PHPMailer instance. |
| 115 |
* @return void |
| 116 |
* @since 1.0.0 |
| 117 |
*/ |
| 118 |
public function custom_phpmailer_init( $PHPMailer ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase -- PHPMailer variable naming is acceptable for this context |
| 119 |
if ( is_object( $PHPMailer ) && method_exists( $PHPMailer, 'IsSMTP' ) ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase,WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase -- PHPMailer property naming is acceptable for this context |
| 120 |
$PHPMailer->IsSMTP(); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase -- PHPMailer variable naming is acceptable for this context |
| 121 |
if ( property_exists( $PHPMailer, 'SMTPAuth' ) ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase,WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase -- PHPMailer property naming is acceptable for this context |
| 122 |
$PHPMailer->SMTPAuth = true; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase,WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase -- PHPMailer property naming is acceptable for this context |
| 123 |
} |
| 124 |
if ( property_exists( $PHPMailer, 'SMTPSecure' ) ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase,WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase -- PHPMailer property naming is acceptable for this context |
| 125 |
$PHPMailer->SMTPSecure = 'ssl'; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase,WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase -- PHPMailer property naming is acceptable for this context |
| 126 |
} |
| 127 |
if ( property_exists( $PHPMailer, 'Host' ) ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase,WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase -- PHPMailer property naming is acceptable for this context |
| 128 |
$PHPMailer->Host = 'smtp.gmail.com'; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase,WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase -- PHPMailer property naming is acceptable for this context |
| 129 |
} |
| 130 |
if ( property_exists( $PHPMailer, 'Port' ) ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase,WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase -- PHPMailer property naming is acceptable for this context |
| 131 |
$PHPMailer->Port = 465; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase,WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase -- PHPMailer property naming is acceptable for this context |
| 132 |
} |
| 133 |
if ( property_exists( $PHPMailer, 'Username' ) ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase,WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase -- PHPMailer property naming is acceptable for this context |
| 134 |
$PHPMailer->Username = 'test@gmail.com'; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase,WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase -- PHPMailer property naming is acceptable for this context |
| 135 |
} |
| 136 |
if ( property_exists( $PHPMailer, 'Password' ) ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase,WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase -- PHPMailer property naming is acceptable for this context |
| 137 |
$PHPMailer->Password = ''; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase,WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase -- PHPMailer property naming is acceptable for this context |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Add email settings in diagnostic information. |
| 144 |
* |
| 145 |
* @return void |
| 146 |
* @since 1.0.0 |
| 147 |
*/ |
| 148 |
public function analytify_settings_logs() { |
| 149 |
echo "\r\n"; |
| 150 |
echo "-- Analytify Email Setting --\r\n \r\n"; |
| 151 |
|
| 152 |
$analytify_email = get_option( 'wp-analytify-email' ); |
| 153 |
|
| 154 |
WPANALYTIFY_Utils::print_settings_array( $analytify_email ); |
| 155 |
} |
| 156 |
} |
| 157 |
|