| 1 |
<?php |
| 2 |
namespace EmailKit\Promotional; |
| 3 |
|
| 4 |
defined( 'ABSPATH' ) || exit; |
| 5 |
/** |
| 6 |
* Global helper class. |
| 7 |
* |
| 8 |
* @since 1.0.0 |
| 9 |
*/ |
| 10 |
|
| 11 |
class Util{ |
| 12 |
|
| 13 |
public static $instance = null; |
| 14 |
private static $key = 'emailkit_options'; |
| 15 |
|
| 16 |
public static function get_option( $key, $default = '' ) { |
| 17 |
$data_all = get_option( self::$key ); |
| 18 |
return ( isset( $data_all[ $key ] ) && $data_all[ $key ] != '' ) ? $data_all[ $key ] : $default; |
| 19 |
} |
| 20 |
|
| 21 |
public static function save_option( $key, $value = '' ) { |
| 22 |
$data_all = get_option( self::$key ) ?? []; |
| 23 |
$data_all[ $key ] = $value; |
| 24 |
return update_option( self::$key, $data_all ); |
| 25 |
} |
| 26 |
|
| 27 |
public static function get_settings( $key, $default = '' ) { |
| 28 |
$data_all = self::get_option( 'settings', array() ); |
| 29 |
return ( isset( $data_all[ $key ] ) && $data_all[ $key ] != '' ) ? $data_all[ $key ] : $default; |
| 30 |
} |
| 31 |
|
| 32 |
public static function save_settings( $new_data = '' ) { |
| 33 |
$data_old = self::get_option( 'settings', array() ); |
| 34 |
$data = array_merge( $data_old, $new_data ); |
| 35 |
return self::save_option( 'settings', $data ); |
| 36 |
} |
| 37 |
|
| 38 |
public static function emailkit_admin_action() { |
| 39 |
// Check for nonce security |
| 40 |
$status = ''; |
| 41 |
|
| 42 |
if (!isset($_POST['nonce']) || ! wp_verify_nonce( sanitize_key(wp_unslash($_POST['nonce'])), 'ajax-nonce' ) ) { |
| 43 |
return; |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 48 |
return; |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
if ( isset( $_POST['settings'] ) ) { |
| 53 |
$status = self::save_settings( empty( $_POST['settings'] ) ? array() : map_deep( wp_unslash( $_POST['settings'] ) , 'sanitize_text_field' ) ); |
| 54 |
} |
| 55 |
|
| 56 |
if($status){ |
| 57 |
wp_send_json_success(); |
| 58 |
}else{ |
| 59 |
wp_send_json_error(); |
| 60 |
} |
| 61 |
exit; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* AJAX action specifically for copy-paste feature |
| 66 |
* Handles enabling/disabling copy-paste functionality with Pro validation |
| 67 |
*/ |
| 68 |
public static function emailkit_copy_paste_action() { |
| 69 |
// Check for nonce security |
| 70 |
if (!isset($_POST['nonce']) || ! wp_verify_nonce( sanitize_key(wp_unslash($_POST['nonce'])), 'ajax-nonce' ) ) { |
| 71 |
wp_send_json_error(['message' => esc_html__( 'Security check failed', 'emailkit' )]); |
| 72 |
exit; |
| 73 |
} |
| 74 |
|
| 75 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 76 |
wp_send_json_error(['message' => esc_html__( 'Insufficient permissions', 'emailkit' )]); |
| 77 |
exit; |
| 78 |
} |
| 79 |
|
| 80 |
// Check if EmailKit Pro is active |
| 81 |
$is_pro_active = is_plugin_active('emailkit-pro/emailkit-pro.php'); |
| 82 |
|
| 83 |
if (!$is_pro_active) { |
| 84 |
wp_send_json_error(['message' => esc_html__( 'EmailKit Pro is required for copy-paste feature', 'emailkit' )]); |
| 85 |
exit; |
| 86 |
} |
| 87 |
|
| 88 |
// Get the copy-paste setting value |
| 89 |
$copy_paste_value = isset($_POST['copy_paste_enabled']) ? sanitize_text_field(wp_unslash($_POST['copy_paste_enabled'])) : 'no'; |
| 90 |
|
| 91 |
// Validate value |
| 92 |
if (!in_array($copy_paste_value, ['yes', 'no'])) { |
| 93 |
wp_send_json_error(['message' => esc_html__( 'Invalid value provided', 'emailkit' )]); |
| 94 |
exit; |
| 95 |
} |
| 96 |
|
| 97 |
// Save the setting |
| 98 |
$status = self::save_settings(['emailkit_enable_copy_paste' => $copy_paste_value]); |
| 99 |
|
| 100 |
if ($status) { |
| 101 |
wp_send_json_success([ |
| 102 |
'message' => esc_html__( 'Copy-paste setting updated successfully', 'emailkit' ), |
| 103 |
'value' => $copy_paste_value |
| 104 |
]); |
| 105 |
} else { |
| 106 |
wp_send_json_error(['message' => esc_html__( 'Failed to save setting', 'emailkit' )]); |
| 107 |
} |
| 108 |
exit; |
| 109 |
} |
| 110 |
|
| 111 |
public static function instance() { |
| 112 |
if ( is_null( self::$instance ) ) { |
| 113 |
|
| 114 |
// Fire the class instance |
| 115 |
self::$instance = new self(); |
| 116 |
} |
| 117 |
|
| 118 |
return self::$instance; |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
/** |
| 123 |
* Get emailkit older version if has any. |
| 124 |
* |
| 125 |
* @since 1.0.0 |
| 126 |
* @access public |
| 127 |
*/ |
| 128 |
public static function old_version(){ |
| 129 |
$version = get_option('emailkit_version'); |
| 130 |
return null == $version ? -1 : $version; |
| 131 |
} |
| 132 |
|
| 133 |
public static function array_push_assoc($array, $key, $value){ |
| 134 |
$array[$key] = $value; |
| 135 |
return $array; |
| 136 |
} |
| 137 |
|
| 138 |
public static function render($content){ |
| 139 |
if (stripos($content, "emailkit-has-lisence") !== false) { |
| 140 |
return null; |
| 141 |
} |
| 142 |
|
| 143 |
return $content; |
| 144 |
} |
| 145 |
|
| 146 |
public static function get_form_settings($key){ |
| 147 |
$options = get_option('emailkit_option__settings'); |
| 148 |
return isset($options[$key]) ? $options[$key] : ''; |
| 149 |
} |
| 150 |
public static function emailkit_content_renderer($content){ |
| 151 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 152 |
echo $content; |
| 153 |
} |
| 154 |
} |
| 155 |
|