class-ajax-functions.php
4 months ago
class-define-constant.php
2 months ago
class-functions.php
3 months ago
do-it.php
2 months ago
inject-script.php
3 months ago
inject-script.php
243 lines
| 1 | <?php |
| 2 | if (! defined('ABSPATH')) { |
| 3 | exit; // Exit if accessed directly |
| 4 | } |
| 5 | |
| 6 | class AVCF_Inject_Script { |
| 7 | private $function; |
| 8 | private $license; |
| 9 | private $is_collab_active; |
| 10 | private $inisetup; |
| 11 | |
| 12 | public function __construct() { |
| 13 | $this->function = new AVCF_Functions(); |
| 14 | $this->license = $this->function->avcf_get_setting_data('avc_license'); |
| 15 | $this->is_collab_active = $this->function->avcf_get_setting_data('avc_collab_active'); |
| 16 | $this->inisetup = $this->function->avcf_get_setting_data('avc_initial_setup_complete'); |
| 17 | |
| 18 | // Initialize hooks |
| 19 | $this->init_hooks(); |
| 20 | } |
| 21 | |
| 22 | private function init_hooks() { |
| 23 | // Load collaboration script |
| 24 | add_action('wp_head', [$this, 'load_collaboration_script'], 11); |
| 25 | add_action('admin_head', [$this, 'load_collaboration_script'], 11); |
| 26 | |
| 27 | |
| 28 | // Load global styles and scripts |
| 29 | add_action('wp_enqueue_scripts', [$this, 'enqueue_global_assets']); |
| 30 | add_action('admin_enqueue_scripts', [$this, 'enqueue_global_assets']); |
| 31 | |
| 32 | // Load footer scripts |
| 33 | add_action('wp_footer', [$this, 'load_footer_script']); |
| 34 | add_action('admin_footer', [$this, 'load_footer_script']); |
| 35 | |
| 36 | // Auto login |
| 37 | add_action('init', array($this, 'avcf_autologin')); |
| 38 | //add_action('init', array($this, 'avcf_accept_invitation')); |
| 39 | } |
| 40 | |
| 41 | public function load_collaboration_script() { |
| 42 | if (isset($_GET['site_id']) && ! empty($_GET['site_id'])) { |
| 43 | return; |
| 44 | } |
| 45 | |
| 46 | $site_id = $this->function->avcf_get_setting_data('avc_site_id'); |
| 47 | if (isset($_GET['activation_callback']) && ! empty($_GET['activation_callback'])) { |
| 48 | $site_id = ''; |
| 49 | } |
| 50 | |
| 51 | $user_id = $this->function->avcf_get_user_detail('id'); |
| 52 | if (is_wp_error($user_id)) { |
| 53 | $user_id = 0; |
| 54 | } |
| 55 | |
| 56 | $is_webmaster = $user_id ? (get_user_meta($user_id, 'avc_user_type', true) === 'webmaster') : false; |
| 57 | $has_consented = $user_id ? (bool) get_user_meta($user_id, 'avc_consent_status', true) : false; |
| 58 | |
| 59 | $allow_collab = false; |
| 60 | if (! is_user_logged_in()) { |
| 61 | $allow_collab = $this->function->avcf_is_site_public($site_id); |
| 62 | } |
| 63 | |
| 64 | if (isset($_GET['collab'])) { |
| 65 | $allow_collab = filter_var($_GET['collab'], FILTER_VALIDATE_BOOLEAN); |
| 66 | } |
| 67 | |
| 68 | $is_setting_screen = $this->function->avcf_setting_screen(); |
| 69 | |
| 70 | if ( |
| 71 | ! $allow_collab && ( |
| 72 | $this->license !== 'valid' || |
| 73 | $this->is_collab_active !== 'yes' || |
| 74 | $this->inisetup !== 'yes' || |
| 75 | ! is_user_logged_in() || |
| 76 | ! $this->function->avcf_allowed_user_role() || |
| 77 | (! $is_webmaster && ! $has_consented) || |
| 78 | $site_id == '' |
| 79 | ) |
| 80 | ) { |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | echo $this->function->get_collab_js($site_id, $is_setting_screen); |
| 85 | } |
| 86 | |
| 87 | public function enqueue_global_assets() { |
| 88 | wp_register_style( |
| 89 | 'avc-global-style', |
| 90 | AVCF_PLUGIN_URL . 'assets/css/global.css', |
| 91 | [], |
| 92 | filemtime(AVCF_PLUGIN_DIR . 'assets/css/global.css') |
| 93 | ); |
| 94 | wp_enqueue_style('avc-global-style'); |
| 95 | |
| 96 | wp_enqueue_script('jquery'); |
| 97 | |
| 98 | wp_register_script( |
| 99 | 'avc-global-script', |
| 100 | AVCF_PLUGIN_URL . 'assets/js/global.js', |
| 101 | ['jquery'], |
| 102 | AVCF_VERSION, |
| 103 | true |
| 104 | ); |
| 105 | wp_enqueue_script('avc-global-script'); |
| 106 | |
| 107 | wp_localize_script('avc-global-script', 'avcajax', [ |
| 108 | 'ajaxurl' => admin_url('admin-ajax.php') |
| 109 | ]); |
| 110 | |
| 111 | $avc_nonce = wp_create_nonce('avc-script-nonce'); |
| 112 | wp_localize_script('avc-global-script', 'avc_site_data', [ |
| 113 | 'site_url' => AVCF_HOME_URL, |
| 114 | 'avc_nonce' => $avc_nonce |
| 115 | ]); |
| 116 | } |
| 117 | |
| 118 | public function load_footer_script() { |
| 119 | $user_id = $this->function->avcf_get_user_detail('id'); |
| 120 | if ( is_wp_error( $user_id ) ) { |
| 121 | $user_id = 0; |
| 122 | } |
| 123 | |
| 124 | $is_webmaster = $user_id ? ( get_user_meta( $user_id, 'avc_user_type', true ) === 'webmaster' ) : false; |
| 125 | $has_consented = $user_id ? (bool) get_user_meta( $user_id, 'avc_consent_status', true ) : false; |
| 126 | $site_id = $this->function->avcf_get_setting_data('avc_site_id'); |
| 127 | |
| 128 | if ( |
| 129 | $this->function->avcf_setting_screen() || |
| 130 | $this->license !== 'valid' || |
| 131 | $this->is_collab_active !== 'yes' || |
| 132 | $this->inisetup !== 'yes' || |
| 133 | ! is_user_logged_in() || |
| 134 | ! $this->function->avcf_allowed_user_role() || |
| 135 | $is_webmaster || |
| 136 | $has_consented || |
| 137 | $site_id == '' |
| 138 | ) { |
| 139 | return; |
| 140 | } |
| 141 | |
| 142 | echo $this->function->avcf_user_consent_modal_trigger(); |
| 143 | echo $this->function->avcf_user_consent_form(); |
| 144 | } |
| 145 | |
| 146 | public function avcf_autologin() { |
| 147 | if (! isset($_GET['wpf_token'])) { |
| 148 | return; |
| 149 | } |
| 150 | |
| 151 | if (isset($_GET['wpf_token']) && is_user_logged_in()) { |
| 152 | return; |
| 153 | } |
| 154 | |
| 155 | $webmaster = $this->function->avcf_get_setting_data('avc_website_developer'); |
| 156 | if ($webmaster == '') { |
| 157 | return; |
| 158 | } |
| 159 | |
| 160 | $payload = [ |
| 161 | 'site_id' => $this->function->avcf_get_setting_data('avc_site_id'), |
| 162 | ]; |
| 163 | |
| 164 | $wpf_token = sanitize_text_field(wp_unslash($_GET['wpf_token'])); |
| 165 | |
| 166 | $response = $this->function->avcf_make_api_call( |
| 167 | AVCF_CRM_API . 'wp-api/user/verify-access', |
| 168 | wp_json_encode($payload), |
| 169 | '', |
| 170 | $wpf_token |
| 171 | ); |
| 172 | |
| 173 | if ( |
| 174 | $response['status_code'] === 200 && |
| 175 | isset($response['data']['status']) && |
| 176 | $response['data']['status'] == 1 |
| 177 | ) { |
| 178 | $user = get_user_by('email', $webmaster); |
| 179 | if (! is_wp_error($user)) { |
| 180 | wp_clear_auth_cookie(); |
| 181 | wp_set_current_user($user->ID); |
| 182 | wp_set_auth_cookie($user->ID); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | $removeparam = array('wpf_token', 'wpf_username', 'wpf_login'); |
| 187 | // Remove the params from the current request URL. |
| 188 | $newurl = remove_query_arg($removeparam); |
| 189 | // Redirect safely (same-host only). |
| 190 | wp_safe_redirect(esc_url_raw($newurl), 302); |
| 191 | exit; |
| 192 | } |
| 193 | |
| 194 | public function avcf_accept_invitation() { |
| 195 | if ( |
| 196 | is_user_logged_in() || |
| 197 | ! isset($_GET['token']) || |
| 198 | empty($_GET['token']) |
| 199 | ) { |
| 200 | return; |
| 201 | } |
| 202 | |
| 203 | $data = [ |
| 204 | 'token' => sanitize_text_field(wp_unslash($_GET['token'])) |
| 205 | ]; |
| 206 | |
| 207 | $response = $this->function->avcf_make_api_call( |
| 208 | AVCF_CRM_API . 'collaborate/site/accept-invitation', |
| 209 | $data, |
| 210 | '', |
| 211 | '', |
| 212 | 'GET' |
| 213 | ); |
| 214 | |
| 215 | if ($response['status_code'] === 200) { |
| 216 | if ( |
| 217 | isset($response['data']['status']) && |
| 218 | $response['data']['status'] == 1 && |
| 219 | isset($response['data']['result']['access_token']) |
| 220 | ) { |
| 221 | $token = $response['data']['result']['access_token']; |
| 222 | setcookie('avc_token', $token, time() + (86400 * 30), '/'); |
| 223 | } else if ( |
| 224 | isset($response['data']['status']) && |
| 225 | $response['data']['status'] == '' && |
| 226 | isset($response['data']['data']['error']) |
| 227 | ) { |
| 228 | echo $response['data']['data']['message']; |
| 229 | die; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | $removeparam = array('token', 'role'); |
| 234 | // Remove the params from the current request URL. |
| 235 | $newurl = remove_query_arg($removeparam); |
| 236 | // Redirect safely (same-host only). |
| 237 | wp_safe_redirect(esc_url_raw($newurl), 302); |
| 238 | exit; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | new AVCF_Inject_Script(); |
| 243 |