| 1 |
<?php // phpcs:ignore Squiz.Commenting.FileComment.Missing -- File doc comment exists |
| 2 |
/** |
| 3 |
* Analytify Settings Fields Trait |
| 4 |
* |
| 5 |
* This trait manages the settings fields configuration and rendering. |
| 6 |
* It was created to separate field management logic from the main settings class, |
| 7 |
* providing a clean interface for defining and managing settings fields. |
| 8 |
* |
| 9 |
* PURPOSE: |
| 10 |
* - Defines settings field configurations |
| 11 |
* - Renders individual field types |
| 12 |
* - Manages field validation and sanitization (e.g. sanitize_text_field for text inputs) |
| 13 |
* - Provides field-related utility methods |
| 14 |
* |
| 15 |
* @package WP_Analytify |
| 16 |
* @subpackage Settings |
| 17 |
* @since 8.0.0 |
| 18 |
* @version 9.0.0 |
| 19 |
*/ |
| 20 |
|
| 21 |
trait Analytify_Settings_Fields { |
| 22 |
/** |
| 23 |
* Get settings fields configuration. |
| 24 |
* |
| 25 |
* Text fields use sanitize_callback (e.g. sanitize_text_field) for proper input sanitization. |
| 26 |
* |
| 27 |
* @since 8.0.0 |
| 28 |
* @version 9.0.0 |
| 29 |
* @return array<string, mixed> |
| 30 |
*/ |
| 31 |
public function get_settings_fields() { |
| 32 |
$ga_properties_and_profiles = WP_ANALYTIFY_FUNCTIONS::fetch_ga_properties(); |
| 33 |
|
| 34 |
$settings_fields = array( |
| 35 |
'wp-analytify-authentication' => array( |
| 36 |
array( |
| 37 |
'name' => 'manual_ua_code', |
| 38 |
'label' => __( 'GA Tracking ID', 'wp-analytify' ), |
| 39 |
'desc' => sprintf( |
| 40 |
// translators: %1$s UA example in code, %2$s G example in code, %3$s line break with trailing space, %4$s gtag label in code. |
| 41 |
esc_html__( |
| 42 |
// phpcs:ignore Generic.Files.LineLength.MaxExceeded -- Single literal for i18n tooling. |
| 43 |
'Manually add the Tracking ID that looks like %1$s or %2$s%3$sOur default tracking method is newly recommended %4$s by Google Analytics.', |
| 44 |
'wp-analytify' |
| 45 |
), |
| 46 |
'<code>UA-XXXXXXXX-XX</code>', |
| 47 |
'<code>G-XXXXXXXXXX</code>', |
| 48 |
'<br /> ', |
| 49 |
'<code>Global Site Tag (gtag.js)</code>' |
| 50 |
), |
| 51 |
'type' => 'text', |
| 52 |
'default' => '', |
| 53 |
), |
| 54 |
), |
| 55 |
'wp-analytify-profile' => array( |
| 56 |
array( |
| 57 |
'name' => 'install_ga_code', |
| 58 |
'label' => __( 'Install Google Analytics tracking code', 'wp-analytify' ), |
| 59 |
'desc' => apply_filters( 'analytify_install_ga_text', __( 'Insert Google Analytics (GA) JavaScript code between the HEADER tags in your website. Uncheck this option if you have already inserted the GA code.', 'wp-analytify' ) ), |
| 60 |
'type' => 'checkbox', |
| 61 |
'default' => 'on', |
| 62 |
), |
| 63 |
array( |
| 64 |
'name' => 'exclude_users_tracking', |
| 65 |
'label' => __( 'Exclude users from tracking', 'wp-analytify' ), |
| 66 |
'desc' => __( 'Don\'t insert the tracking code for the above user roles.', 'wp-analytify' ), |
| 67 |
'type' => 'chosen', |
| 68 |
'default' => array(), |
| 69 |
'options' => $this->get_current_roles(), |
| 70 |
), |
| 71 |
array( |
| 72 |
'name' => 'profile_for_posts', |
| 73 |
'label' => __( 'Profile for posts (Backend/Front-end)', 'wp-analytify' ), |
| 74 |
'desc' => sprintf( |
| 75 |
// translators: %1$s opening line break and strong, %2$s closing strong and space before GA4 sentence. |
| 76 |
esc_html__( |
| 77 |
'Select your Google Analytics website profile/stream for Analytify front-end/back-end statistics.%1$sNote:%2$s GA4 properties from the above list.', |
| 78 |
'wp-analytify' |
| 79 |
), |
| 80 |
'<br /><strong>', |
| 81 |
'</strong> ' |
| 82 |
), |
| 83 |
'type' => 'select_profile', |
| 84 |
'default' => 'Choose profile for posts', |
| 85 |
'options' => $ga_properties_and_profiles, |
| 86 |
'size' => '', |
| 87 |
), |
| 88 |
array( |
| 89 |
'name' => 'profile_for_dashboard', |
| 90 |
'label' => __( 'Profile for dashboard', 'wp-analytify' ), |
| 91 |
'desc' => sprintf( |
| 92 |
// translators: %1$s, %2$s: documentation link open and close around link text. |
| 93 |
esc_html__( |
| 94 |
'Select your Google Analytics website profile/stream for Analytify dashboard statistics. Note: Not seeing new GA4 properties in the above list? See %1$swhy and how to fix it%2$s.', |
| 95 |
'wp-analytify' |
| 96 |
), |
| 97 |
'<a href="' . esc_url( 'https://analytify.io/doc/how-to-integrate-analytify-with-google-analytics-4-ga4/' ) . '" target="_blank" rel="noopener noreferrer">', |
| 98 |
'</a>' |
| 99 |
), |
| 100 |
'type' => 'select_profile', |
| 101 |
'default' => 'Choose profile for dashboard', |
| 102 |
'options' => $ga_properties_and_profiles, |
| 103 |
), |
| 104 |
array( |
| 105 |
'name' => 'hide_profiles_list', |
| 106 |
'label' => __( 'Hide profiles list', 'wp-analytify' ), |
| 107 |
'desc' => __( 'Hide the selection of profiles for the back-end/front-end dashboard and posts. You might want to do this so clients cannot see other profiles available.', 'wp-analytify' ), |
| 108 |
'type' => 'checkbox', |
| 109 |
), |
| 110 |
), |
| 111 |
'wp-analytify-admin' => array( |
| 112 |
array( |
| 113 |
'name' => 'enable_back_end', |
| 114 |
'label' => __( 'Enable analytics under posts/pages (wp-admin)', 'wp-analytify' ), |
| 115 |
'desc' => __( 'Disable if you don\'t want to load statistics on all pages by default.', 'wp-analytify' ), |
| 116 |
'type' => 'checkbox', |
| 117 |
), |
| 118 |
array( |
| 119 |
'name' => 'show_analytics_roles_back_end', |
| 120 |
'label' => __( 'Display analytics to roles (posts & pages)', 'wp-analytify' ), |
| 121 |
'desc' => __( 'Show analytics under posts and pages to the above selected user roles only.', 'wp-analytify' ), |
| 122 |
'type' => 'chosen', |
| 123 |
'default' => array(), |
| 124 |
'options' => $this->get_current_roles(), |
| 125 |
), |
| 126 |
array( |
| 127 |
'name' => 'show_analytics_post_types_back_end', |
| 128 |
'label' => __( 'Analytics on post types', 'wp-analytify' ), |
| 129 |
'desc' => class_exists( 'WP_Analytify_Pro' ) |
| 130 |
? __( 'Show Analytics under the above post types only', 'wp-analytify' ) |
| 131 |
: sprintf( |
| 132 |
// translators: %1$s, %2$s: Premium upsell link open and close. |
| 133 |
esc_html__( |
| 134 |
'Show analytics below these post types only. Buy %1$sPremium%2$s version for Custom Post Types.', |
| 135 |
'wp-analytify' |
| 136 |
), |
| 137 |
'<a href="' . esc_url( analytify_get_update_link() ) . '" target="_blank" rel="noopener noreferrer">', |
| 138 |
'</a>' |
| 139 |
), |
| 140 |
'type' => 'chosen', |
| 141 |
'default' => array(), |
| 142 |
'options' => $this->get_current_post_types(), |
| 143 |
), |
| 144 |
array( |
| 145 |
'name' => 'show_panels_back_end', |
| 146 |
'label' => __( 'Edit posts/pages analytics panels', 'wp-analytify' ), |
| 147 |
'desc' => class_exists( 'WP_Analytify_Pro' ) |
| 148 |
? __( 'Select which statistic panels you want to display under posts/pages.', 'wp-analytify' ) |
| 149 |
: sprintf( |
| 150 |
// translators: %1$s, %2$s: Premium upsell link open and close. |
| 151 |
esc_html__( |
| 152 |
// phpcs:ignore Generic.Files.LineLength.MaxExceeded -- Single literal for i18n tooling. |
| 153 |
'Select which statistic panels you want to display under posts/pages. Only "General Stats" will visible in Free Version. Buy %1$sPremium%2$s version to see the full statistics.', |
| 154 |
'wp-analytify' |
| 155 |
), |
| 156 |
'<a href="' . esc_url( analytify_get_update_link() ) . '" target="_blank" rel="noopener noreferrer">', |
| 157 |
'</a>' |
| 158 |
), |
| 159 |
'type' => 'chosen', |
| 160 |
'default' => array(), |
| 161 |
'options' => array( |
| 162 |
'show-overall-dashboard' => __( 'General Stats', 'wp-analytify' ), |
| 163 |
'show-geographic-dashboard' => __( 'Geographic Stats', 'wp-analytify' ), |
| 164 |
'show-system-stats' => __( 'System Stats', 'wp-analytify' ), |
| 165 |
'show-keywords-dashboard' => __( 'Keywords Stats', 'wp-analytify' ), |
| 166 |
'show-social-dashboard' => __( 'Social Media Stats', 'wp-analytify' ), |
| 167 |
'show-referrer-dashboard' => __( 'Referrers Stats', 'wp-analytify' ), |
| 168 |
'show-scroll-depth-stats' => __( 'Scroll Depth', 'wp-analytify' ), |
| 169 |
'show-video-tracking-stats' => __( 'Video Tracking', 'wp-analytify' ), |
| 170 |
'show-what-happen-stats' => __( 'Entrance Exits Stats', 'wp-analytify' ), |
| 171 |
), |
| 172 |
), |
| 173 |
array( |
| 174 |
'name' => 'exclude_pages_back_end', |
| 175 |
'label' => __( 'Exclude analytics on specific pages', 'wp-analytify' ), |
| 176 |
'desc' => __( 'Enter a comma-separated list of the post/page ID\'s you do not want to display analytics for. For example: 21,44,66', 'wp-analytify' ), |
| 177 |
'type' => 'text', |
| 178 |
'default' => '0', |
| 179 |
), |
| 180 |
), |
| 181 |
'wp-analytify-advanced' => array( |
| 182 |
array( |
| 183 |
'name' => 'ga4_web_data_stream', |
| 184 |
'label' => __( 'Data Streams', 'wp-analytify' ), |
| 185 |
'desc' => apply_filters( 'analytify_gtag_tracking_mode_text', __( 'Choose GA4 data/web stream to use for website tracking.', 'wp-analytify' ) ), |
| 186 |
'type' => 'select_streams', |
| 187 |
'options' => WPANALYTIFY_Utils::fetch_ga4_streams(), |
| 188 |
), |
| 189 |
array( |
| 190 |
'name' => 'measurement_protocol_secret', |
| 191 |
'label' => __( 'Measurement Protocol Secret', 'wp-analytify' ), |
| 192 |
'desc' => __( 'Analytify creates measurement protocol secret itself, however you can also create your own secret key.', 'wp-analytify' ), |
| 193 |
'type' => 'text', |
| 194 |
'class' => 'measurement_protocol_secret', |
| 195 |
), |
| 196 |
array( |
| 197 |
'name' => 'user_advanced_keys', |
| 198 |
'label' => __( 'Setup Custom API keys?', 'wp-analytify' ), |
| 199 |
'desc' => sprintf( |
| 200 |
// translators: %1$s line break (reused). %2$s Google Console link open, %3$s closing </a> (reused). %4$s video guide link open. |
| 201 |
esc_html__( |
| 202 |
// phpcs:ignore Generic.Files.LineLength.MaxExceeded -- Single literal for i18n tooling. |
| 203 |
'It is highly recommended by Google to use your own API keys.%1$s You need to create a Project in Google %2$sConsole%3$s.%1$s Here is a short %4$svideo guide%3$s to get your own Client ID, Client Secret and Redirect URL and enter them in the inputs below.', |
| 204 |
'wp-analytify' |
| 205 |
), |
| 206 |
'<br />', |
| 207 |
'<a href="' . esc_url( 'https://console.developers.google.com/project' ) . '" target="_blank" rel="noopener noreferrer">', |
| 208 |
'</a>', |
| 209 |
'<a href="' . esc_url( 'https://analytify.io/custom-api-keys-video' ) . '" target="_blank" rel="noopener noreferrer">' |
| 210 |
), |
| 211 |
'type' => 'checkbox', |
| 212 |
'class' => 'user_advanced_keys', |
| 213 |
), |
| 214 |
array( |
| 215 |
'name' => 'client_id', |
| 216 |
'label' => __( 'Client ID', 'wp-analytify' ), |
| 217 |
'desc' => __( 'Your Client ID', 'wp-analytify' ), |
| 218 |
'type' => 'text', |
| 219 |
'class' => 'user_keys', |
| 220 |
'sanitize_callback' => 'trim', |
| 221 |
), |
| 222 |
array( |
| 223 |
'name' => 'client_secret', |
| 224 |
'label' => __( 'Client secret', 'wp-analytify' ), |
| 225 |
'desc' => __( 'Your Client Secret', 'wp-analytify' ), |
| 226 |
'type' => 'text', |
| 227 |
'class' => 'user_keys', |
| 228 |
'sanitize_callback' => 'trim', |
| 229 |
), |
| 230 |
array( |
| 231 |
'name' => 'redirect_uri', |
| 232 |
'label' => __( 'Redirect URL', 'wp-analytify' ), |
| 233 |
'desc' => sprintf( |
| 234 |
// translators: %1$s bold block containing the admin settings URL as text. |
| 235 |
esc_html__( |
| 236 |
'( Redirect URL is very important when you are using your own keys. Paste this into the above field: %1$s )', |
| 237 |
'wp-analytify' |
| 238 |
), |
| 239 |
'<strong>' . esc_html( admin_url( 'admin.php?page=analytify-settings' ) ) . '</strong>' |
| 240 |
), |
| 241 |
'type' => 'text', |
| 242 |
'class' => 'user_keys', |
| 243 |
'sanitize_callback' => 'trim', |
| 244 |
), |
| 245 |
), |
| 246 |
); |
| 247 |
|
| 248 |
// Dynamically append more advanced fields. |
| 249 |
$advance_setting_fields = array( |
| 250 |
array( |
| 251 |
'name' => 'locally_host_analytics', |
| 252 |
'label' => __( 'Host Google Analytics Locally', 'wp-analytify' ), |
| 253 |
'desc' => __( 'Hosting Google Analytics locally may improve your site speed and other core web vitals.', 'wp-analytify' ), |
| 254 |
'type' => 'checkbox', |
| 255 |
), |
| 256 |
array( |
| 257 |
'name' => 'track_user_id', |
| 258 |
'label' => __( 'Track User ID', 'wp-analytify' ), |
| 259 |
'desc' => sprintf( |
| 260 |
// translators: %1$s, %2$s: Google help link open and close around "here". |
| 261 |
esc_html__( |
| 262 |
'Detailed information about Track User ID in Google Analytics can be found %1$shere%2$s.', |
| 263 |
'wp-analytify' |
| 264 |
), |
| 265 |
'<a href="' . esc_url( 'https://support.google.com/analytics/answer/3123662' ) . '" target="_blank" rel="noopener noreferrer">', |
| 266 |
'</a>' |
| 267 |
), |
| 268 |
'type' => 'checkbox', |
| 269 |
), |
| 270 |
array( |
| 271 |
'name' => 'depth_percentage', |
| 272 |
'label' => __( 'Scroll Depth', 'wp-analytify' ), |
| 273 |
'desc' => __( 'Track page scroll depth percentage. This will help you figure out the most highlighted area of the page. Percentage events are fired at the 25%, 50%, 75%, and 100% scrolling points', 'wp-analytify' ), |
| 274 |
'type' => 'checkbox', |
| 275 |
), |
| 276 |
array( |
| 277 |
'name' => 'video_tracking', |
| 278 |
'label' => __( 'Video Tracking', 'wp-analytify' ), |
| 279 |
'desc' => __( 'Track embedded video interactions such as play, pause, and completion. This will help you understand user engagement with video content.', 'wp-analytify' ), |
| 280 |
'type' => 'checkbox', |
| 281 |
), |
| 282 |
array( |
| 283 |
'name' => 'demographic_interest_tracking', |
| 284 |
'label' => __( 'Demographic & Interest Tracking', 'wp-analytify' ), |
| 285 |
'desc' => __( 'This allows you to view extra dimensions about users: Age, gender, affinity categories, in-market segments, etc.', 'wp-analytify' ), |
| 286 |
'type' => 'checkbox', |
| 287 |
), |
| 288 |
array( |
| 289 |
'name' => '404_page_track', |
| 290 |
'label' => __( 'Page Not Found (404)', 'wp-analytify' ), |
| 291 |
'desc' => __( 'Track all 404 pages.', 'wp-analytify' ), |
| 292 |
'type' => 'checkbox', |
| 293 |
), |
| 294 |
array( |
| 295 |
'name' => 'javascript_error_track', |
| 296 |
'label' => __( 'JavaScript Errors', 'wp-analytify' ), |
| 297 |
'desc' => __( 'Track all JavaScript errors.', 'wp-analytify' ), |
| 298 |
'type' => 'checkbox', |
| 299 |
), |
| 300 |
array( |
| 301 |
'name' => 'ajax_error_track', |
| 302 |
'label' => __( 'AJAX Errors', 'wp-analytify' ), |
| 303 |
'desc' => __( 'Track all AJAX errors.', 'wp-analytify' ), |
| 304 |
'type' => 'checkbox', |
| 305 |
), |
| 306 |
array( |
| 307 |
'name' => 'linker_cross_domain_tracking', |
| 308 |
'label' => __( 'Setup Cross-domain Tracking', 'wp-analytify' ), |
| 309 |
'desc' => sprintf( |
| 310 |
// translators: %1$s code sample, %2$s and %3$s guide link open and close. |
| 311 |
esc_html__( |
| 312 |
'This will add the %1$s tag to your tracking code. Read this %2$sguide%3$s for more information.', |
| 313 |
'wp-analytify' |
| 314 |
), |
| 315 |
'<code>allowLinker:true</code>', |
| 316 |
'<a href="' . esc_url( 'https://analytify.io/doc/setup-cross-domain-tracking-wordpress' ) . '" target="_blank" rel="noopener noreferrer">', |
| 317 |
'</a>' |
| 318 |
), |
| 319 |
'type' => 'checkbox', |
| 320 |
'class' => 'user_linker_tracking', |
| 321 |
), |
| 322 |
array( |
| 323 |
'name' => 'linked_domain', |
| 324 |
'label' => __( 'Domain', 'wp-analytify' ), |
| 325 |
'desc' => __( 'All the linked domains separated by a comma', 'wp-analytify' ), |
| 326 |
'type' => 'text', |
| 327 |
'class' => 'linker_tracking', |
| 328 |
'sanitize_callback' => 'sanitize_text_field', |
| 329 |
), |
| 330 |
/** |
| 331 |
* Exclude URL Query Parameters: when enabled, the list below strips those query keys |
| 332 |
* from the page URL before sending to GA. Only query-string keys are removed; path and hash unchanged. |
| 333 |
* |
| 334 |
* @since 9.0.0 |
| 335 |
*/ |
| 336 |
array( |
| 337 |
'name' => 'exclude_query_params', |
| 338 |
'label' => __( 'Exclude URL Query Parameters', 'wp-analytify' ), |
| 339 |
'desc' => __( 'Filter out specific URL query parameters from being tracked inside Google Analytics.', 'wp-analytify' ), |
| 340 |
'type' => 'checkbox', |
| 341 |
'class' => 'user_exclude_query_params', |
| 342 |
), |
| 343 |
array( |
| 344 |
'name' => 'query_params_to_exclude', |
| 345 |
'label' => __( 'Exclude Query Parameters', 'wp-analytify' ), |
| 346 |
'desc' => __( 'Enter comma-separated query parameters to exclude (e.g. fbclid, ref). Only letters, numbers, underscores and hyphens are allowed.', 'wp-analytify' ), |
| 347 |
'type' => 'text', |
| 348 |
'class' => 'exclude_query_params', |
| 349 |
'sanitize_callback' => 'analytify_sanitize_query_params_to_exclude', |
| 350 |
), |
| 351 |
array( |
| 352 |
'name' => 'enable_token_refresh_failure_email', |
| 353 |
'label' => __( 'Reauthentication Email Alert', 'wp-analytify' ), |
| 354 |
'desc' => __( 'Get an email notification when your site needs you to sign in again due to a Google error or revoked refresh token.', 'wp-analytify' ), |
| 355 |
'type' => 'checkbox', |
| 356 |
'default' => 'off', |
| 357 |
), |
| 358 |
array( |
| 359 |
'name' => 'uninstall_analytify_settings', |
| 360 |
'label' => __( 'Remove All Data On Uninstall', 'wp-analytify' ), |
| 361 |
'desc' => __( 'Upon uninstall, this will remove all defined settings and their data from your website and reset them to default.', 'wp-analytify' ), |
| 362 |
'type' => 'checkbox', |
| 363 |
), |
| 364 |
array( |
| 365 |
'name' => 'custom_js_code', |
| 366 |
'label' => __( 'Custom JavaScript Code', 'wp-analytify' ), |
| 367 |
'desc' => __( 'This will add inline tracking code before sending the pageview hit to Google Analytics.', 'wp-analytify' ), |
| 368 |
'type' => 'textarea', |
| 369 |
), |
| 370 |
array( |
| 371 |
'name' => 'custom_css_code', |
| 372 |
'label' => __( 'Custom CSS Code', 'wp-analytify' ), |
| 373 |
'desc' => __( 'This will add inline css code in admin dashboard pages of Analytify', 'wp-analytify' ), |
| 374 |
'type' => 'textarea', |
| 375 |
), |
| 376 |
); |
| 377 |
|
| 378 |
foreach ( $advance_setting_fields as $advance_setting_field ) { |
| 379 |
array_push( $settings_fields['wp-analytify-advanced'], $advance_setting_field ); |
| 380 |
} |
| 381 |
|
| 382 |
// Filter for pro. |
| 383 |
$settings_fields = apply_filters( 'wp_analytify_pro_setting_fields', $settings_fields ); |
| 384 |
return $settings_fields; |
| 385 |
} |
| 386 |
} |
| 387 |
|