2021-12-27 01:15:36 +01:00
< ? php
/*
Plugin Name : Wide Angle Analytics
Plugin URI : https :// wordpress . org / plugins / wide - angle - analytics /
Description : Easily enable and configure Wide Angle Analytics on your Wordpress site
Author : Wide Angle Analytics by Input Objects GmbH
Author URI : https :// wideangle . co
2024-05-01 22:11:05 +00:00
Version : 1.0 . 8
2021-12-27 01:15:36 +01:00
Requires at least : 5.2
2021-12-27 15:21:41 +01:00
Requires PHP : 7.2
2021-12-27 01:15:36 +01:00
License : GPL v2
License URI : https :// www . gnu . org / licenses / gpl - 2.0 . html
*/
?>
< ? php
class WideAngleAnalytics {
const WAA_CONF_SITE_ID = " waa_site_id " ;
const WAA_CONF_TRACKER_DOMAIN = " waa_tracker_domain " ;
2022-01-18 23:18:06 +01:00
const WAA_CONF_FINGERPRINT = " waa_fingerprint " ;
2021-12-27 01:15:36 +01:00
const WAA_CONF_EXC_PATHS = " waa_exc_path " ;
const WAA_CONF_INC_PARAMS = " waa_inc_params " ;
const WAA_CONF_IGNORE_HASH = " waa_ignore_hash " ;
2024-05-01 22:11:05 +00:00
const WAA_CONF_SUPRESS_DNT = " waa_supress_dnt " ;
2021-12-30 12:58:35 +01:00
const WAA_CONF_ATTRIBUTES = " waa_attributes " ;
2021-12-27 01:15:36 +01:00
public function __construct () {
$this -> plugin = new stdClass ;
$this -> plugin -> name = 'wide-angle-analytics' ;
$this -> plugin -> displayName = 'Wide Angle Analytics' ;
$this -> plugin -> folder = plugin_dir_path ( __FILE__ );
include_once ( $this -> plugin -> folder . '/types/WideAngleHelpers.php' );
2021-12-30 12:58:35 +01:00
include_once ( $this -> plugin -> folder . '/types/WideAngleGenerator.php' );
2021-12-27 01:15:36 +01:00
$this -> plugin -> helpers = new WideAngleHelpers ();
$this -> plugin -> exclusionTypes = array (
" start " => " Starts with " ,
" end " => " Ends with " ,
" regex " => " RegEx " ,
);
2022-01-24 00:09:38 +01:00
$this -> plugin -> ePrivacyModes = array (
" disabled " => " Disable Tracking " ,
" consent " => " Track assuming consent "
);
2021-12-29 22:39:17 +01:00
add_action ( 'admin_init' , array ( & $this , 'registerPluginSettings' ) );
add_action ( 'admin_menu' , array ( & $this , 'registerAdminMenu' ));
2022-01-02 15:29:52 +01:00
$this -> plugin -> generator = new WideAngleGenerator ( get_option ( self :: WAA_CONF_ATTRIBUTES ));
2021-12-27 01:15:36 +01:00
add_action ( 'wp_head' , array ( & $this , 'renderHeaderScript' ));
add_action ( 'wp_footer' , array ( & $this , 'renderFooterScript' ));
}
/**
* When script is configured and saved , function will render prefetch script directive .
* Inteded to be used with 'wp_head' hook .
*/
function renderHeaderScript () {
2022-01-02 15:29:52 +01:00
$this -> renderContent ( $this -> plugin -> generator -> generateHeaderScript ()); // The Escaping takes place in the WideAngleGenerator::generateHeaderScript method.
2021-12-27 01:15:36 +01:00
}
/**
* When script is configured and saved , function will render Wide Angle Analytics script .
* Inteded to be used with 'wp_footer' hook .
*/
function renderFooterScript () {
2022-01-02 15:29:52 +01:00
$this -> renderContent ( $this -> plugin -> generator -> generateFooterScript ()); // The Escaping takes place in the WideAngleGenerator::generateFooterScript method.
2021-12-27 01:15:36 +01:00
}
/**
2021-12-30 12:58:35 +01:00
* Helper function to render provided content " as-is " when rendering public facing page .
*
* The $content is already escaped .
2021-12-27 01:15:36 +01:00
*/
2021-12-30 12:58:35 +01:00
private function renderContent ( $content ) {
2021-12-27 01:15:36 +01:00
if (
is_admin () ||
is_feed () ||
is_robots () ||
is_trackback ()) {
return ;
}
2021-12-30 12:58:35 +01:00
if ( trim ( $content ) === " " ) {
2021-12-27 01:15:36 +01:00
return ;
}
2021-12-30 12:58:35 +01:00
echo wp_unslash ( $content );
2021-12-27 01:15:36 +01:00
}
/**
* Adds Wide Angle Analytics configuration menu to Admin " Settings " menu .
*/
function registerAdminMenu () {
add_submenu_page (
'options-general.php' ,
$this -> plugin -> displayName ,
$this -> plugin -> displayName ,
'manage_options' ,
$this -> plugin -> name ,
array ( & $this , 'adminPanelHandler' )
);
}
/**
* Handle configuration submission .
*
* When successful , the generated script will rendered and storedin plugin keyed setting .
* This setting will be subsequently used when time comes to render it . The intermediate settings
* are parsed and processed only during configuration change .
*
*/
function adminPanelHandler () {
if ( ! current_user_can ( 'manage_options' ) ) {
wp_die ( __ ( 'Insufficient permissions. You are not allows to modify setting.' , $this -> plugin -> displayName ) );
}
if ( isset ( $_REQUEST [ 'submit' ] ) ) {
if ( ! isset ( $_REQUEST [ $this -> plugin -> name . '_nonce' ] ) ) {
$this -> errorMessage = __ ( 'Nonce field is missing. Settings NOT saved.' , $this -> plugin -> name );
} elseif ( ! wp_verify_nonce ( $_REQUEST [ $this -> plugin -> name . '_nonce' ], $this -> plugin -> name ) ) {
$this -> errorMessage = __ ( 'Invalid nonce specified. Settings NOT saved.' , $this -> plugin -> name );
} else {
2021-12-30 20:55:20 +01:00
$waaSiteId = $this -> plugin -> helpers -> validateSiteId ( self :: WAA_CONF_SITE_ID , sanitize_text_field ( $_REQUEST [ 'waa_site_id' ]));
$waaTrackerDomain = $this -> plugin -> helpers -> validateTrackerDomain ( self :: WAA_CONF_TRACKER_DOMAIN , sanitize_text_field ( $_REQUEST [ 'waa_tracker_domain' ]));
$waaIgnoreHash = $this -> plugin -> helpers -> validateIgnoreHashFlag ( self :: WAA_CONF_IGNORE_HASH , sanitize_text_field ( $_REQUEST [ 'waa_ignore_hash' ]));
2022-01-18 23:18:06 +01:00
$waaFingerprint = $this -> plugin -> helpers -> validateFingerprint ( self :: WAA_CONF_FINGERPRINT , sanitize_text_field ( $_REQUEST [ 'waa_fingerprint' ]));
2024-05-01 22:11:05 +00:00
$waaSupressDnt = $this -> plugin -> helpers -> validateSupressDntFlag ( self :: WAA_CONF_SUPRESS_DNT , sanitize_text_field ( $_REQUEST [ 'waa_supress_dnt' ]));
2021-12-30 12:58:35 +01:00
$waaIncParams = $this -> plugin -> helpers -> validateIncludeParams ( self :: WAA_CONF_INC_PARAMS , $_REQUEST );
$waaExclusionPaths = $this -> plugin -> helpers -> validateExclusionPathsRequest ( self :: WAA_CONF_EXC_PATHS , $_REQUEST );
2021-12-29 02:09:21 +01:00
2021-12-30 12:58:35 +01:00
include_once ( $this -> plugin -> folder . '/types/WideAngleAttributes.php' );
2024-05-01 22:11:05 +00:00
$merged = array ( $waaSiteId , $waaTrackerDomain , $waaIgnoreHash , $waaIncParams , $waaExclusionPaths , $waaSupressDnt );
2021-12-29 02:09:21 +01:00
$errors = array ();
foreach ( $merged as $validated ) {
if ( ! $validated -> is_valid ()) {
array_push ( $errors , $validated -> get_error ());
}
}
if ( count ( $errors ) === 0 ) {
2022-01-18 23:18:06 +01:00
$attributes = new WideAngleAttributes (
$waaSiteId -> get_value (),
$waaTrackerDomain -> get_value (),
$waaIgnoreHash -> get_value (),
$waaExclusionPaths -> get_value (),
$waaIncParams -> get_value (),
2022-01-24 00:09:38 +01:00
$waaFingerprint -> get_value (),
2024-05-01 22:11:05 +00:00
$waaSupressDnt -> get_value ()
2022-01-18 23:18:06 +01:00
);
2021-12-30 12:58:35 +01:00
update_option ( self :: WAA_CONF_SITE_ID , $waaSiteId -> get_value ());
update_option ( self :: WAA_CONF_TRACKER_DOMAIN , $waaTrackerDomain -> get_value ());
update_option ( self :: WAA_CONF_IGNORE_HASH , $waaIgnoreHash -> get_value ());
update_option ( self :: WAA_CONF_EXC_PATHS , $waaExclusionPaths -> get_value ());
update_option ( self :: WAA_CONF_INC_PARAMS , $waaIncParams -> get_value ());
2022-01-18 23:18:06 +01:00
update_option ( self :: WAA_CONF_FINGERPRINT , $waaFingerprint -> get_value ());
2024-05-01 22:11:05 +00:00
update_option ( self :: WAA_CONF_SUPRESS_DNT , $waaSupressDnt -> get_value ());
2021-12-30 12:58:35 +01:00
update_option ( self :: WAA_CONF_ATTRIBUTES , $attributes -> generateAttributes ());
2021-12-29 02:09:21 +01:00
$this -> message = __ ( 'Settings updated' , $this -> plugin -> name );
} else {
$this -> errorMessage = $errors ;
}
2021-12-27 01:15:36 +01:00
}
}
$this -> settings = array (
2021-12-30 12:58:35 +01:00
self :: WAA_CONF_SITE_ID => get_option ( self :: WAA_CONF_SITE_ID ),
self :: WAA_CONF_EXC_PATHS => get_option ( self :: WAA_CONF_EXC_PATHS ),
self :: WAA_CONF_INC_PARAMS => get_option ( self :: WAA_CONF_INC_PARAMS ),
self :: WAA_CONF_TRACKER_DOMAIN => get_option ( self :: WAA_CONF_TRACKER_DOMAIN ),
self :: WAA_CONF_IGNORE_HASH => get_option ( self :: WAA_CONF_IGNORE_HASH ),
2022-01-18 23:18:06 +01:00
self :: WAA_CONF_FINGERPRINT => get_option ( self :: WAA_CONF_FINGERPRINT ),
2024-05-01 22:11:05 +00:00
self :: WAA_CONF_SUPRESS_DNT => get_option ( self :: WAA_CONF_SUPRESS_DNT ),
2021-12-30 12:58:35 +01:00
self :: WAA_CONF_ATTRIBUTES => get_option ( self :: WAA_CONF_ATTRIBUTES )
2021-12-27 01:15:36 +01:00
);
include_once ( $this -> plugin -> folder . '/views/admin_settings.php' );
}
/**
* Creates settings for Wide Angle Analytics plugin .
*
* Following settings are registered :
* - waa_site_id
* - waa_tracker_domain
* - waa_exc_path
* - waa_inc_params
* - waa_ignore_hash
2024-05-01 22:11:05 +00:00
* - waa_supress_dnt
2021-12-27 01:15:36 +01:00
* - waa_header_script
* - waa_footer_script
*/
function registerPluginSettings () {
2021-12-29 02:09:21 +01:00
register_setting ( $this -> plugin -> name , self :: WAA_CONF_SITE_ID );
register_setting ( $this -> plugin -> name , self :: WAA_CONF_EXC_PATHS );
register_setting ( $this -> plugin -> name , self :: WAA_CONF_INC_PARAMS );
register_setting ( $this -> plugin -> name , self :: WAA_CONF_TRACKER_DOMAIN , array ( 'default' => 'stats.wideangle.co' ));
register_setting ( $this -> plugin -> name , self :: WAA_CONF_IGNORE_HASH , array ( 'default' => 'false' ));
2022-01-18 23:18:06 +01:00
register_setting ( $this -> plugin -> name , self :: WAA_CONF_FINGERPRINT , array ( 'default' => 'false' ));
2024-05-01 22:11:05 +00:00
register_setting ( $this -> plugin -> name , self :: WAA_CONF_SUPRESS_DNT , array ( 'default' => 'false' ));
2021-12-30 12:58:35 +01:00
register_setting ( $this -> plugin -> name , self :: WAA_CONF_ATTRIBUTES , array ( 'type' => 'array' ));
2021-12-27 01:15:36 +01:00
}
}
$waa = new WideAngleAnalytics (); // Plugin is initialized on contruction.
?>