Plugin Directory

Changeset 2398796


Ignore:
Timestamp:
10/13/2020 03:45:55 PM (5 years ago)
Author:
quentn
Message:

Web tracking code updated in WP when user updates settings in Quentn

Location:
quentn-wp/trunk
Files:
1 added
6 edited

Legend:

Unmodified
Added
Removed
  • quentn-wp/trunk/admin/class-quentn-wp-admin.php

    r2371136 r2398796  
    351351        $settings = array(
    352352            array(
    353                 'option_group' => 'web_tracking_options_group',
    354                 'option_name'  => 'quentn_web_tracking_enabled'
    355             ),
    356             array(
    357                 'option_group' => 'web_tracking_options_group',
    358                 'option_name'  => 'quentn_web_tracking_consent_method'
    359             ),
    360             array(
    361353                'option_group' => 'quentn_tags_options_group',
    362354                'option_name'  => 'quentn_tags_wp_user'
     
    374366        //add values for settings api, add_settings_section
    375367        $sections = array(
    376             array(
    377                 'id'        => 'quentn_web_tracking_option',
    378                 'title'     =>  __( 'Web Tracking Settings', 'quentn-wp'),
    379                 'callback'  => '__return_false',
    380                 'page'      => 'quentn-dashboard-web-tracking'
    381             ),
    382368            array(
    383369                'id'       => 'quentn_tags_option',
     
    416402        $fields = array();
    417403        $fields[] = array(
    418             'id'        => 'quentn_web_tracking_enabled',
    419             'title'     =>  __( 'Web Tracking', 'quentn-wp' ),
    420             'callback'  => array( $this, 'field_quentn_web_tracking' ),
    421             'page'      => 'quentn-dashboard-web-tracking',
    422             'section'   => 'quentn_web_tracking_option',
    423         );
    424 
    425         if( $this->is_confirmation_required_for_web_tracking() ) {
    426             $fields[] = array(
    427                 'id'        => 'quentn_web_tracking_consent_method',
    428                 'title'     => __('Consent Method', 'quentn-wp'),
    429                 'callback'  => array( $this, 'field_quentn_consent_method' ),
    430                 'page'      => 'quentn-dashboard-web-tracking',
    431                 'section'   => 'quentn_web_tracking_option',
    432             );
    433         }
    434 
    435         $fields[] = array(
    436404            'id'        => 'quentn_auto_login_redirect_url',
    437405            'title'     => __( 'Redirect URL', 'quentn-wp' ),
     
    814782
    815783    /**
    816      * Display web tracking enabled field
    817      *
    818      * @since  1.0.0
    819      * @access public
    820      * @return void
    821      */
    822     public function field_quentn_web_tracking()
    823     {
    824         $value = esc_attr( get_option( 'quentn_web_tracking_enabled' ) );
    825         ?>
    826         <input type="checkbox" class="form-control" value="1" name="quentn_web_tracking_enabled" id="quentn_web_tracking_enabled" <?php checked( $value); disabled( ! $this->api_handler->is_connected_with_quentn() || ! $this->api_handler->is_web_tracking_enabled() || ! $this->is_domain_registered( $_SERVER['HTTP_HOST'], $this->api_handler->get_registered_domains() )  ); ?>>
    827         <?php
    828     }
    829 
    830     /**
    831      * Display web tracking consent methods dropdown
    832      *
    833      * @since  1.0.0
    834      * @access public
    835      * @return void
    836      */
    837     public function field_quentn_consent_method()
    838     {
    839         $value = esc_attr( get_option( 'quentn_web_tracking_consent_method' ) );
    840         ?>
    841         <select name="quentn_web_tracking_consent_method" id="quentn_web_tracking_consent_method" <?php disabled(  ! get_option( 'quentn_web_tracking_enabled'  ) || ! $this->api_handler->is_connected_with_quentn() || ! $this->api_handler->is_web_tracking_enabled() || ! $this->is_domain_registered( $_SERVER['HTTP_HOST'], $this->api_handler->get_registered_domains() ) ); ?>>
    842             <option value="confirm-by-server" <?php  selected( $value, 'confirm-by-server' ); ?>><?php _e( 'Confirm By Server', 'quentn-wp' ) ?></option>
    843             <option value="cookie-notice" <?php  selected( $value, 'cookie-notice' ); disabled( ! Helper::is_cookie_notice_plugin_enabled() ) ?>>Cookie Notice</option>
    844             <option value="quentn-overlay" <?php  selected( $value, 'quentn-overlay' ); ?>><?php _e('Quentn Overlay', 'quentn-wp' ) ?></option>
    845         </select>
    846         <?php
    847     }
    848 
    849     /**
    850      * Display web tracking consent methods dropdown
     784     * Display redirect url after auto login
    851785     *
    852786     * @since  1.1.0
     
    1040974
    1041975    /**
    1042      * Searches the array for a given domain and returns the ID of domain if found
    1043      *
    1044      * @since  1.0.0
    1045      * @access public
    1046      * @param string $needle_domain the searched domain
    1047      * @param array $haystack_domains
    1048      * @return int|bool
    1049      */
    1050     public function get_domain_id( $needle_domain, $haystack_domains ) {
    1051 
    1052         foreach( $haystack_domains as $domain ) {
    1053             if ( $this->compare_domain( $needle_domain, $domain['domain'] ) ) {
    1054                 return  $domain['id'];
    1055             }
    1056         }
    1057         return false;
    1058     }
    1059 
    1060     /**
    1061      * Searches the array for a given value
    1062      *
    1063      * @since  1.0.0
    1064      * @access public
    1065      * @param string $needle_domain the searched domain
    1066      * @param array $haystack_domains
    1067      * @return bool
    1068      */
    1069     public function is_domain_registered( $needle_domain, $haystack_domains )
    1070     {
    1071         foreach ( $haystack_domains as $domain ) {
    1072             if ( $this->compare_domain( $needle_domain, $domain ) ) {
    1073                 return true;
    1074             }
    1075         }
    1076         return false;
    1077     }
    1078 
    1079     /**
    1080      * Compare the two domain
    1081      *
    1082      * @since  1.0.0
    1083      * @access public
    1084      * @param string $domain_1
    1085      * @param string $domain_2
    1086      * @param int $level the level upto which the domain will be compared
    1087      * @return bool
    1088      */
    1089     public function compare_domain( $domain_1, $domain_2, $level = 0 ) {
    1090 
    1091         //add http:// if needed
    1092         $domain_1 = ( parse_url( $domain_1, PHP_URL_SCHEME ) ) ? $domain_1 : 'http://'.$domain_1;
    1093         $domain_2 = ( parse_url( $domain_2, PHP_URL_SCHEME ) ) ? $domain_2 : 'http://'.$domain_2;
    1094 
    1095         //skip domain upto specific level
    1096         $domain_1_host = $this->skip_subdomain_upto_specific_level( parse_url( $domain_1, PHP_URL_HOST ), $level );
    1097         $domain_2_host = $this->skip_subdomain_upto_specific_level( parse_url( $domain_2, PHP_URL_HOST ), $level );
    1098 
    1099         if ( $domain_1_host == $domain_2_host ) {
    1100             return true;
    1101         }
    1102         return false;
    1103     }
    1104 
    1105     /**
    1106      * Skip domain parts to specifc level
    1107      *
    1108      * @since  1.0.0
    1109      * @access public
    1110      * @param string $host url to skip
    1111      * @param int $level the level to skip the parts
    1112      * @return string
    1113      */
    1114     public function skip_subdomain_upto_specific_level( $host, $level = 0 ) {
    1115 
    1116         $host_data = explode( ".", $host );
    1117 
    1118         //if host contains www , ignore it by adding one more level to skip
    1119         if( $host_data[0] === 'www' ) {
    1120             $level += 1;
    1121         }
    1122         //check if host contains subdomains
    1123         if ( count( $host_data  ) > 2 ) {
    1124             //skip sudomains upto given level
    1125             for ( $x = 0; $x < $level; $x++ ) {
    1126                 unset( $host_data[$x] );
    1127                 //keep alteast last two part of host e.g example.com
    1128                 if( count( $host_data ) == 2 ) {
    1129                     break;
    1130                 }
    1131             }
    1132         }
    1133         return implode( ".", $host_data );
    1134     }
    1135 
    1136 
    1137     /**
    1138      * Get web tracking code
    1139      *
    1140      * @since  1.0.0
    1141      * @access public
    1142      * @return string
    1143      */
    1144     public function get_quentn_web_tracking_code() {
    1145         //get web tracking response
    1146         $response = $this->api_handler->get_web_tracking_response();
    1147         //if domains are not found then return empty string
    1148         if( ! isset( $response['data']['domains'] ) ) {
    1149             return '';
    1150         }
    1151         //get domain ID
    1152         $domain_id = $this->get_domain_id( $_SERVER['HTTP_HOST'], $response['data']['domains'] );
    1153 
    1154         if ( ! $domain_id ) {
    1155             return '';
    1156         }
    1157         //get array key from domain ID
    1158         if ( $domain_id ) {
    1159             $domain_key = array_search($domain_id, array_column($response['data']['domains'], 'id'));
    1160         }
    1161 
    1162          $tracking_host_url = isset( $response['data']['trackingHostUrl'] ) ? $response['data']['trackingHostUrl'] : '';
    1163          $system_host_url = isset( $response['data']['systemHostUrl'] ) ? $response['data']['systemHostUrl'] : '';
    1164          $js_source = isset( $response['data']['js_source'] ) ? $response['data']['js_source'] : '';
    1165          $domain_data = isset( $response['data']['domains'][$domain_key] ) ? $response['data']['domains'][$domain_key] : '';
    1166 
    1167         $set_values_data = array(
    1168             'idSite'             =>     $domain_data['piwik_site_id'],
    1169             'trackingHostUrl'    =>     "'$tracking_host_url'",
    1170             'trackAnonymusUser'  =>     $domain_data['track_anonymous'],
    1171             'creq'               =>     $domain_data['confirmation_required'],
    1172             'systemHostUrl'      =>     "'$system_host_url'",
    1173         );
    1174 
    1175         //set tracking values in required format for quentn
    1176         $set_value_string = $this->set_tracking_values( $set_values_data );
    1177 
    1178         $web_tracking = "<!-- Quentn tracking code -->
    1179             <script>
    1180                 var _qntn = _qntn || [];
    1181                 {$set_value_string}
    1182                 (function (d, s, id, q){
    1183                     if (d.readyState === 'complete') {
    1184                         q.push(['domReady']);
    1185                     } else {
    1186                         d.addEventListener('DOMContentLoaded', function () {
    1187                         q.push(['domReady']);
    1188                     });
    1189                    }
    1190                     var js, fjs = d.getElementsByTagName(s)[0];
    1191                     if (d.getElementById(id))
    1192                         return;
    1193                     js = d.createElement(s);
    1194                     js.id = id;
    1195                     js.src = '{$js_source}';
    1196                     fjs.parentNode.insertBefore(js, fjs);
    1197                 }(document, 'script', 'quentn-tracking-jssdk', _qntn));
    1198                 </script><!-- /Quentn tracking code -->";
    1199 
    1200                 if( $domain_data['confirmation_required'] && get_option( 'quentn_web_tracking_consent_method' ) == 'quentn-overlay' && isset( $domain_data['confirmation_overlay'] ) ) {
    1201                     $web_tracking.= $domain_data['confirmation_overlay'];
    1202                 }elseif($domain_data['confirmation_required'] && get_option('quentn_web_tracking_consent_method')=='cookie-notice' && Helper::is_cookie_notice_plugin_enabled()) {
    1203                     $web_tracking.= "<!-- Quentn bridge to Cookie Notice -->
    1204                     <script>
    1205                     ( function ( $ ) {
    1206                         $( document ).ready( function () {
    1207                                 _qntn.push(['getConfirmation', function(status) {
    1208                                     if (status && !$.fn.getCookieNotice()) {
    1209                                         $.fn.setCookieNotice('accept');
    1210                                     }
    1211                                 }]);
    1212                             } );
    1213                         $( document ).on( 'setCookieNotice', function ( e) {
    1214                             _qntn.push(['setConfirmation', true]);
    1215                         } );
    1216                     } )( jQuery );
    1217                     </script>
    1218                         <!-- /Quentn bridge to Cookie Notice -->";
    1219                 }
    1220                 return $web_tracking;
    1221     }
    1222 
    1223     /**
    1224      * Check if web tracking is enabled at Quentn host
    1225      *
    1226      * @since  1.0.0
    1227      * @access public
    1228      * @return bool
    1229      */
    1230     public function is_confirmation_required_for_web_tracking()
    1231     {
    1232         $return = false;
    1233         //get web tracking response
    1234         $response = $this->api_handler->get_web_tracking_response();
    1235 
    1236         if( ! isset( $response['data']['domains'] ) ) {
    1237             return $return;
    1238         }
    1239         //get domain ID among all registered domain at quentn host
    1240         $domain_id = $this->get_domain_id( $_SERVER['HTTP_HOST'], $response['data']['domains'] );
    1241         //get array index
    1242         if ( ! $domain_id ) {
    1243             return $return;
    1244         }
    1245 
    1246         $domain_key = array_search( $domain_id, array_column( $response['data']['domains'], 'id' ) );
    1247 
    1248         $domain_data = $response['data']['domains'][$domain_key];
    1249 
    1250         if( isset( $domain_data['confirmation_required'] ) && $domain_data['confirmation_required'] ) {
    1251             $return = true;
    1252         }
    1253 
    1254         return $return;
    1255     }
    1256 
    1257     /**
    1258      * Convert tracking values into string format to print in web analytics code
    1259      *
    1260      * @since  1.0.0
    1261      * @access private
    1262      * @param array $data
    1263      * @return string
    1264      */
    1265     private function set_tracking_values( $data ) {
    1266         $return = '';
    1267         foreach ( $data as $key=>$value ) {
    1268             if( $value ) {
    1269                 //convert 1 to true for javascript text
    1270                 $val = ( $value==1 ) ? 'true' : $value;
    1271                 $return.= "_qntn.push(['setValue','{$key}', {$val}]);\n";
    1272             }
    1273         }
    1274         return $return;
    1275     }
    1276 
    1277     /**
    1278976     * Set screen options
    1279977     *
  • quentn-wp/trunk/admin/partials/quentn-wp-dashboard.php

    r2371136 r2398796  
    5454            echo '</form>';
    5555        } elseif ( $active_tab == "quentn_web_tracking_tab" ) { //display web tracking options
     56            $web_tracking = new Quentn_Wp_Web_Tracking();
    5657            echo '<form method="post" action="options.php">';
    5758            if( get_option('quentn_web_tracking_enabled') ) {
    58                 update_option("quentn_web_tracking_code", $this->get_quentn_web_tracking_code() );
     59                update_option("quentn_web_tracking_code", $web_tracking->get_quentn_web_tracking_code() );
    5960            } else { //if web tracking option is disabled, we will delete previously saved tracking code
    6061                delete_option('quentn_web_tracking_code');
     
    7071                );
    7172            }
    72             elseif( $this->api_handler->is_connected_with_quentn() && ! $this->is_domain_registered( $_SERVER['HTTP_HOST'], $this->api_handler->get_registered_domains() ) ) {
     73            elseif( $this->api_handler->is_connected_with_quentn() && ! $web_tracking->is_domain_registered( $_SERVER['HTTP_HOST'], $this->api_handler->get_registered_domains() ) ) {
    7374                if ( ! empty( $this->api_handler->error_messages ) ) {
    7475                    $this->show_errors( $this->api_handler->error_messages );
  • quentn-wp/trunk/includes/class-quentn-wp-rest-api-controller.php

    r2371136 r2398796  
    6060     */
    6161    private $get_user_roles;
     62
     63    /**
     64     * The base URL for route to update web tracking code
     65     *
     66     * @since  1.1.1
     67     * @access private
     68     * @var    string
     69     */
     70    private $get_tracking;
    6271
    6372
     
    7786        $this->get_page_restrictions = '/get-page-restrictions';
    7887        $this->get_user_roles = '/get-user-roles';
    79 
     88        $this->get_tracking = '/get-tracking';
    8089    }
    8190
     
    199208            // Here we register our callback. The callback is fired when this endpoint is matched by the WP_REST_Server class.
    200209            'callback' => array( $this, 'quentn_create_user' ),
     210            // Here we register our permissions callback. The callback is fired before the main callback to check if the current user can access the endpoint.
     211            'permission_callback' => array( $this, 'quentn_check_credentials' ),
     212
     213            'args' => array(
     214                'data' => array(
     215                    'required' => true,
     216                    'type' => 'string',
     217                ),
     218                'vu' => array(
     219                    'required' => true,
     220                    'type' => 'integer',
     221                ),
     222            ),
     223
     224        ));
     225
     226        //register route to get tracking code when settings is saved in Quentn
     227        register_rest_route( $this->namespace, $this->get_tracking, array(
     228            // By using this constant we ensure that when the WP_REST_Server changes our readable endpoints will work as intended.
     229            'methods' => \WP_REST_Server::CREATABLE,
     230            // Here we register our callback. The callback is fired when this endpoint is matched by the WP_REST_Server class.
     231            'callback' => array( $this, 'quentn_get_tracking_code' ),
    201232            // Here we register our permissions callback. The callback is fired before the main callback to check if the current user can access the endpoint.
    202233            'permission_callback' => array( $this, 'quentn_check_credentials' ),
     
    305336        }
    306337        return rest_ensure_response( json_encode( $restricted_pages ) );
    307 
    308 
    309338    }
    310339
     
    359388                //insert user in wordpress
    360389                $user_id = wp_insert_user( $qn_userdata );
     390                // On success, add user meta last login as false
     391                if ( ! is_wp_error( $user_id ) ) {
     392                    update_user_meta( $user_id, 'quentn_last_login', 0 );
     393                }
    361394            }
    362395
     
    385418
    386419            do_action( 'quentn_user_register', $new_user );
    387             //add user meta last login as false
    388             update_user_meta( $new_user->ID, 'quentn_last_login', 0 );
    389420
    390421            //send email if set by quentn call
     
    399430
    400431        return rest_ensure_response( 'Data Successfully Updated' );
     432    }
     433
     434    /**
     435     * Get list of all wp roles
     436     *
     437     * @since  1.1.1
     438     * @access public
     439     * @return array
     440     */
     441    public function quentn_get_tracking_code( ) {
     442        $web_tracking = new Quentn_Wp_Web_Tracking();
     443        if( ! get_option('quentn_web_tracking_enabled') ) {
     444            return rest_ensure_response( array( 'saved' => 0 ) );
     445        }
     446        update_option("quentn_web_tracking_code", $web_tracking->get_quentn_web_tracking_code() );
     447        return rest_ensure_response( array( 'saved' => 1 ) );
    401448    }
    402449
  • quentn-wp/trunk/includes/class-quentn-wp.php

    r2371136 r2398796  
    7575            $this->version = QUENTN_WP_VERSION;
    7676        } else {
    77             $this->version = '1.1.0';
     77            $this->version = '1.1.1';
    7878        }
    7979        $this->plugin_name = 'quentn-wp';
     
    158158
    159159        require_once plugin_dir_path( dirname( __FILE__ ) ) . 'vendor/autoload.php';
     160
     161        /**
     162         * The class responsible for handling web tracking code
     163         */
     164        require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-quentn-wp-web-tracking.php';
    160165
    161166        /**
  • quentn-wp/trunk/quentn-wp.php

    r2371136 r2398796  
    1717 * Plugin URI:        https://docs.quentn.com/de/beta-quentn-wordpress-plugin/installieren-und-verbinden
    1818 * Description:       This plugin allows you to restrict access to specific pages, create custom access links and create dynamic page countdowns. Optionally, you can connect your Quentn account to your WordPress installation to share contacts and manage access restrictions through Quentn.
    19  * Version:           1.1.0
     19 * Version:           1.1.1
    2020 * Author:            Quentn.com GmbH
    2121 * Author URI:        https://quentn.com/
     
    3535define( "TABLE_QUENTN_RESTRICTIONS", 'qntn_restrictions' );
    3636define( "TABLE_QUENTN_USER_DATA", 'qntn_user_data' );
    37 define( 'QUENTN_WP_VERSION', '1.1.0' );
     37define( 'QUENTN_WP_VERSION', '1.1.1' );
    3838
    3939/**
  • quentn-wp/trunk/readme.txt

    r2371136 r2398796  
    44Requires at least: 4.6.0
    55Tested up to: 5.4.2
    6 Stable tag: 1.1.0
     6Stable tag: 1.1.1
    77Requires PHP: 5.6.0
    88License: GPLv2 or later
     
    6666
    6767== Changelog ==
     68= 1.1.1 =
     69* Add: Web tracking code updated in WP when user updates settings in Quentn.
     70
    6871= 1.1.0 =
    6972* Fix: Error when activate Elementor network wide in multisite.
     
    106109
    107110== Upgrade Notice ==
     111= 1.1.1 =
     112Thanks for using Quentn Plugin! Please update the plugin to update web tracking code in WP when user updates settings in Quentn.
    108113
    109114= 1.1.0 =
Note: See TracChangeset for help on using the changeset viewer.