| 1 |
<?php |
| 2 |
/** |
| 3 |
* Ask Permission |
| 4 |
* |
| 5 |
* @package Copy the Code |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! class_exists( 'Copy_The_Code_Ask_Permission' ) ) : |
| 9 |
|
| 10 |
/** |
| 11 |
* Ask Permission |
| 12 |
*/ |
| 13 |
class Copy_The_Code_Ask_Permission { |
| 14 |
|
| 15 |
/** |
| 16 |
* Instance |
| 17 |
* |
| 18 |
* @access private |
| 19 |
* @var object Class Instance. |
| 20 |
*/ |
| 21 |
private static $instance; |
| 22 |
|
| 23 |
/** |
| 24 |
* Initiator |
| 25 |
* |
| 26 |
* @return object initialized object of class. |
| 27 |
*/ |
| 28 |
public static function get_instance() { |
| 29 |
if ( ! isset( self::$instance ) ) { |
| 30 |
self::$instance = new self; |
| 31 |
} |
| 32 |
return self::$instance; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Constructor |
| 37 |
*/ |
| 38 |
public function __construct() { |
| 39 |
add_action( 'admin_init', array( $this, 'add_settings_field' ) ); |
| 40 |
add_action( 'admin_notices', array( $this, 'admin_notice' ) ); |
| 41 |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); |
| 42 |
add_action( 'wp_ajax_copy_the_code_allow_tracking', array( $this, 'allow_tracking' ) ); |
| 43 |
add_action( 'wp_ajax_copy_the_code_dont_allow_tracking', array( $this, 'dont_allow_tracking' ) ); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Allow tracking. |
| 48 |
*/ |
| 49 |
public function allow_tracking() { |
| 50 |
check_ajax_referer( 'copy-the-code-ask-permission', 'security' ); |
| 51 |
|
| 52 |
update_option( 'copy_the_code_allow_tracking', 'yes' ); |
| 53 |
|
| 54 |
wp_send_json_success(); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Allow tracking. |
| 59 |
*/ |
| 60 |
public function dont_allow_tracking() { |
| 61 |
check_ajax_referer( 'copy-the-code-ask-permission', 'security' ); |
| 62 |
|
| 63 |
update_option( 'copy_the_code_allow_tracking', 'no' ); |
| 64 |
|
| 65 |
set_transient( 'copy_the_code_ask_again', 'no', MONTH_IN_SECONDS ); |
| 66 |
|
| 67 |
wp_send_json_success(); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Enqueue Scripts |
| 72 |
*/ |
| 73 |
public function enqueue_scripts() { |
| 74 |
wp_enqueue_script( 'copy-the-code-notice', COPY_THE_CODE_URI . 'assets/js/notice.js', array( 'jquery' ), COPY_THE_CODE_VER, true ); |
| 75 |
wp_localize_script( |
| 76 |
'copy-the-code-notice', |
| 77 |
'copy_the_code_notice', |
| 78 |
array( |
| 79 |
'ajax_url' => admin_url( 'admin-ajax.php' ), |
| 80 |
'nonce' => wp_create_nonce( 'copy-the-code-ask-permission' ), |
| 81 |
) |
| 82 |
); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Add notice. |
| 87 |
*/ |
| 88 |
public function admin_notice() { |
| 89 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 90 |
return; |
| 91 |
} |
| 92 |
|
| 93 |
$allow = get_option( 'copy_the_code_allow_tracking', 'no' ); |
| 94 |
if ( 'yes' === $allow ) { |
| 95 |
return; |
| 96 |
} |
| 97 |
|
| 98 |
$transient = get_transient( 'copy_the_code_ask_again' ); |
| 99 |
if ( 'no' === $transient ) { |
| 100 |
return; |
| 101 |
} |
| 102 |
?> |
| 103 |
<div class="copy-the-code-notice notice notice-info is-dismissible"> |
| 104 |
<p><?php echo wp_kses_post( __( 'Want to help make our products more awesome? Allow us to collect non-sensitive diagnostic data and usage information.', 'copy-the-code' ) ); ?></p> |
| 105 |
<p> |
| 106 |
<a href="#" class="button button-primary copy-the-code-allow-tracking"><?php echo esc_html( __( 'Yes! Allow it', 'copy-the-code' ) ); ?></a> |
| 107 |
<a href="#" class="button copy-the-code-not-allow-tracking"><?php echo esc_html( __( 'No thanks', 'copy-the-code' ) ); ?></a> |
| 108 |
</p> |
| 109 |
</div> |
| 110 |
<?php |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Add settings field. |
| 115 |
*/ |
| 116 |
public function add_settings_field() { |
| 117 |
add_settings_field( |
| 118 |
'copy_the_code_allow_tracking', |
| 119 |
__( 'Enable tracking', 'copy-the-code' ), |
| 120 |
array( $this, 'settings_field' ), |
| 121 |
'general' |
| 122 |
); |
| 123 |
register_setting( 'general', 'copy_the_code_allow_tracking' ); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Settings field. |
| 128 |
*/ |
| 129 |
public function settings_field() { |
| 130 |
$value = get_option( 'copy_the_code_allow_tracking', 'no' ); |
| 131 |
?> |
| 132 |
<label> |
| 133 |
<input type="checkbox" name="copy_the_code_allow_tracking" value="yes" <?php checked( $value, 'yes' ); ?> /> |
| 134 |
<?php _e( 'Allow usage of Copy the Code to be tracked', 'copy-the-code' ); ?><br/> |
| 135 |
<p class="description">To opt out, leave this box unticked. Your store remains untracked, and no data will be collected. Read about what usage data is tracked at: <a href="https://surror.com/usage-tracking/" target="_blank">Usage Tracking Documentation</a>.</p> |
| 136 |
</label> |
| 137 |
<?php |
| 138 |
} |
| 139 |
|
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Kicking this off by calling 'get_instance()' method |
| 144 |
*/ |
| 145 |
Copy_The_Code_Ask_Permission::get_instance(); |
| 146 |
|
| 147 |
endif; |
| 148 |
|