gtm4wp_sanitize_options

The Google Tag Manager for WordPress gtm4wp sanitize options function.

Description

gtm4wp_sanitize_options( $options ); 

Parameters (1)

0. $options
The options.

Usage

  1. if ( !function_exists( 'gtm4wp_sanitize_options' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'google-tag-manager-for-wordpress/admin/admin.php'; 
  3.  
  4. // The options. 
  5. $options = null; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = gtm4wp_sanitize_options($options); 
  9.  

Defined (1)

The function is defined in the following location(s).

/admin/admin.php  
  1. function gtm4wp_sanitize_options($options) { 
  2. global $wpdb; 
  3.  
  4. $output = gtm4wp_reload_options(); 
  5.  
  6. foreach($output as $optionname => $optionvalue) { 
  7. if ( isset( $options[$optionname] ) ) { 
  8. $newoptionvalue = $options[$optionname]; 
  9. } else { 
  10. $newoptionvalue = ""; 
  11.  
  12. // "include" settings 
  13. if ( substr($optionname, 0, 8) == "include-" ) { 
  14. $output[$optionname] = (boolean) $newoptionvalue; 
  15.  
  16. // tracked download extensions 
  17. } else if ( $optionname == GTM4WP_OPTION_EVENTS_DWLEXT ) { 
  18. $output[$optionname] = str_replace( " ", "", trim( $newoptionvalue ) ); 
  19.  
  20. // dataLayer events 
  21. } else if ( substr($optionname, 0, 6) == "event-" ) { 
  22. $output[$optionname] = (boolean) $newoptionvalue; 
  23.  
  24. // clear oembed transients whenfeatureis enabled because we need to hook into the oembed process to enable some 3rd party APIs 
  25. if ( $output[$optionname] && !$optionvalue ) { 
  26. if ( GTM4WP_OPTION_EVENTS_YOUTUBE == $optionname ) { 
  27. $wpdb->query( "DELETE FROM $wpdb->postmeta WHERE meta_value LIKE '%youtube.com%' AND meta_key LIKE '_oembed_%'" ); 
  28.  
  29. if ( GTM4WP_OPTION_EVENTS_VIMEO == $optionname ) { 
  30. $wpdb->query( "DELETE FROM $wpdb->postmeta WHERE meta_value LIKE '%vimeo.com%' AND meta_key LIKE '_oembed_%'" ); 
  31.  
  32. // integrations 
  33. } else if ( substr($optionname, 0, 10) == "integrate-" ) { 
  34. $output[$optionname] = (boolean) $newoptionvalue; 
  35.  
  36. // GTM code or dataLayer variable name 
  37. } else if ( ( $optionname == GTM4WP_OPTION_GTM_CODE ) || ( $optionname == GTM4WP_OPTION_DATALAYER_NAME ) ) { 
  38. $newoptionvalue = trim($newoptionvalue); 
  39.  
  40. // if ( ( $optionname == GTM4WP_OPTION_GTM_CODE ) && ( ! preg_match( "/^GTM-[A-Z0-9]+$/", $newoptionvalue ) ) ) { 
  41. // add_settings_error( GTM4WP_ADMIN_GROUP, GTM4WP_OPTIONS . '[' . GTM4WP_OPTION_GTM_CODE . ']', __( "Invalid Google Tag Manager ID. ValidIDformat: GTM-XXXXX", 'duracelltomi-google-tag-manager' ) ); 
  42. if ( $optionname == GTM4WP_OPTION_GTM_CODE ) { 
  43. $_gtmid_list = explode( ", ", $newoptionvalue ); 
  44. $_gtmid_haserror = false; 
  45.  
  46. foreach( $_gtmid_list as $one_gtm_id ) { 
  47. $_gtmid_haserror = $_gtmid_haserror || !preg_match( "/^GTM-[A-Z0-9]+$/", $one_gtm_id ); 
  48.  
  49. if ( $_gtmid_haserror ) { 
  50. add_settings_error( GTM4WP_ADMIN_GROUP, GTM4WP_OPTIONS . '[' . GTM4WP_OPTION_GTM_CODE . ']', __( "Invalid Google Tag Manager ID. ValidIDformat: GTM-XXXXX. Use comma without additional space (, ) to enter more than one container ID.", 'duracelltomi-google-tag-manager' ) ); 
  51. } else { 
  52. $output[$optionname] = $newoptionvalue; 
  53. } else if ( ( $optionname == GTM4WP_OPTION_DATALAYER_NAME ) && ( $newoptionvalue != "" ) && ( ! preg_match( "/^[a-zA-Z][a-zA-Z0-9_-]*$/", $newoptionvalue ) ) ) { 
  54. add_settings_error( GTM4WP_ADMIN_GROUP, GTM4WP_OPTIONS . '[' . GTM4WP_OPTION_DATALAYER_NAME . ']', __( "Invalid dataLayer variable name. Please start with a character from a-z or A-Z followed by characters from a-z, A-Z, 0-9 or '_' or '-'!", 'duracelltomi-google-tag-manager' ) ); 
  55.  
  56. } else { 
  57. $output[$optionname] = $newoptionvalue; 
  58.  
  59. // GTM container code placement 
  60. } else if ( $optionname == GTM4WP_OPTION_GTM_PLACEMENT ) { 
  61. $output[$optionname] = (int) $newoptionvalue; 
  62. if ( ( $output[$optionname] < 0) || ( $output[$optionname] > 3 ) ) { 
  63. $output[$optionname] = 0; 
  64.  
  65. // scroll tracking content ID 
  66. } else if ( $optionname == GTM4WP_OPTION_SCROLLER_CONTENTID ) { 
  67. $output[$optionname] = trim( str_replace( "#", "", $newoptionvalue ) ); 
  68. // anything else 
  69. } else { 
  70. switch( gettype($optionvalue)) { 
  71. case "boolean": { 
  72. $output[$optionname] = (boolean) $newoptionvalue; 
  73.  
  74. break; 
  75.  
  76. case "integer": { 
  77. $output[$optionname] = (int) $newoptionvalue; 
  78.  
  79. break; 
  80.  
  81. default: { 
  82. $output[$optionname] = $newoptionvalue; 
  83. } // end switch 
  84.  
  85. return $output;