Yoast_GA_Options

Options class.

Defined (1)

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

/includes/class-options.php  
  1. class Yoast_GA_Options { 
  2.  
  3. /** @var array */ 
  4. public $options; 
  5.  
  6. /** 
  7. * Holds the settings for the GA plugin and possible subplugins 
  8. * @var string 
  9. */ 
  10. public $option_name = 'yst_ga'; 
  11.  
  12. /** 
  13. * Holds the prefix we use within the option to save settings 
  14. * @var string 
  15. */ 
  16. public $option_prefix = 'ga_general'; 
  17.  
  18. /** 
  19. * Holds the path to the main plugin file 
  20. * @var string 
  21. */ 
  22. public $plugin_path; 
  23.  
  24. /** 
  25. * Holds the URL to the main plugin directory 
  26. * @var string 
  27. */ 
  28. public $plugin_url; 
  29.  
  30. /** 
  31. * Saving instance of it's own in this static var 
  32. * @var object 
  33. */ 
  34. private static $instance; 
  35.  
  36. /** 
  37. * Getting instance of this object. If instance doesn't exists it will be created. 
  38. * @return object|Yoast_GA_Options 
  39. */ 
  40. public static function instance() { 
  41.  
  42. if ( is_null( self::$instance ) ) { 
  43. self::$instance = new Yoast_GA_Options(); 
  44.  
  45. return self::$instance; 
  46.  
  47.  
  48. /** 
  49. * Constructor for the options 
  50. */ 
  51. public function __construct() { 
  52. $this->options = $this->get_options(); 
  53. $this->options = $this->check_options( $this->options ); 
  54.  
  55. $this->plugin_path = plugin_dir_path( GAWP_FILE ); 
  56. $this->plugin_url = trailingslashit( plugin_dir_url( GAWP_FILE ) ); 
  57.  
  58. if ( false == $this->options ) { 
  59. add_option( $this->option_name, $this->default_ga_values() ); 
  60. $this->options = $this->get_options(); 
  61.  
  62. if ( ! isset( $this->options['version'] ) || $this->options['version'] < GAWP_VERSION ) { 
  63. $this->upgrade(); 
  64.  
  65. // If instance is null, create it. Prevent creating multiple instances of this class 
  66. if ( is_null( self::$instance ) ) { 
  67. self::$instance = $this; 
  68.  
  69. add_action( 'plugins_loaded', array( $this, 'load_textdomain' ) ); 
  70.  
  71. /** 
  72. * Updates the GA option within the current option_prefix 
  73. * @param array $val 
  74. * @return bool 
  75. */ 
  76. public function update_option( $val ) { 
  77. $options = get_option( $this->option_name ); 
  78. $options[ $this->option_prefix ] = $val; 
  79.  
  80. return update_option( $this->option_name, $options ); 
  81.  
  82. /** 
  83. * Return the Google Analytics options 
  84. * @return mixed|void 
  85. */ 
  86. public function get_options() { 
  87. $options = get_option( $this->option_name ); 
  88.  
  89. return $options[ $this->option_prefix ]; 
  90.  
  91. /** 
  92. * Check if all the options are set, to prevent a notice if debugging is enabled 
  93. * When we have new changes, the settings are saved to the options class 
  94. * @param array $options 
  95. * @return mixed 
  96. */ 
  97. public function check_options( $options ) { 
  98.  
  99. $changes = 0; 
  100. foreach ( $this->default_ga_values() as $key => $value ) { 
  101. if ( ! isset( $options[ $key ] ) ) { 
  102. $options[ $key ] = $value; 
  103. $changes ++; 
  104.  
  105. if ( $changes >= 1 ) { 
  106. $this->update_option( $options ); 
  107.  
  108. return $options; 
  109.  
  110. /** 
  111. * Get the Google Analytics tracking code for this website 
  112. * @return null 
  113. */ 
  114. public function get_tracking_code() { 
  115. $tracking_code = null; 
  116. $this->options = $this->get_options(); 
  117.  
  118. if ( ! empty( $this->options['analytics_profile'] ) && ! empty( $this->options['analytics_profile_code'] ) ) { 
  119. $tracking_code = $this->options['analytics_profile_code']; 
  120. elseif ( ! empty( $this->options['analytics_profile'] ) && empty( $this->options['analytics_profile_code'] ) ) { 
  121. // Analytics profile is still holding the UA code 
  122. $tracking_code = $this->options['analytics_profile']; 
  123.  
  124. if ( ! empty( $this->options['manual_ua_code_field'] ) && ! empty( $this->options['manual_ua_code'] ) ) { 
  125. $tracking_code = $this->options['manual_ua_code_field']; 
  126.  
  127. return $tracking_code; 
  128.  
  129. /** 
  130. * Convert a option value to a bool 
  131. * @param string $option_name 
  132. * @return bool 
  133. */ 
  134. public function option_value_to_bool( $option_name ) { 
  135. $this->options = $this->get_options(); 
  136.  
  137. if ( isset( $this->options[ $option_name ] ) && $this->options[ $option_name ] == 1 ) { 
  138. return true; 
  139.  
  140. return false; 
  141.  
  142. /** 
  143. * Upgrade the settings when settings are changed. 
  144. * @since 5.0.1 
  145. */ 
  146. private function upgrade() { 
  147. if ( ! isset( $this->options['version'] ) && is_null( $this->get_tracking_code() ) ) { 
  148. $old_options = get_option( 'Yoast_Google_Analytics' ); 
  149. if ( isset( $old_options ) && is_array( $old_options ) ) { 
  150. if ( isset( $old_options['uastring'] ) && '' !== trim( $old_options['uastring'] ) ) { 
  151. // Save UA as manual UA, instead of saving all the old GA crap 
  152. $this->options['manual_ua_code'] = 1; 
  153. $this->options['manual_ua_code_field'] = $old_options['uastring']; 
  154. // Other settings 
  155. $this->options['allow_anchor'] = $old_options['allowanchor']; 
  156. $this->options['add_allow_linker'] = $old_options['allowlinker']; 
  157. $this->options['anonymous_data'] = $old_options['anonymizeip']; 
  158. $this->options['track_outbound'] = $old_options['trackoutbound']; 
  159. $this->options['track_internal_as_outbound'] = $old_options['internallink']; 
  160. $this->options['track_internal_as_label'] = $old_options['internallinklabel']; 
  161. $this->options['extensions_of_files'] = $old_options['dlextensions']; 
  162. delete_option( 'Yoast_Google_Analytics' ); 
  163. // 5.0.0 to 5.0.1 fix of ignore users array 
  164. if ( ! isset( $this->options['version'] ) || version_compare( $this->options['version'], '5.0.1', '<' ) ) { 
  165. if ( isset( $this->options['ignore_users'] ) && ! is_array( $this->options['ignore_users'] ) ) { 
  166. $this->options['ignore_users'] = (array) $this->options['ignore_users']; 
  167. // 5.1.2+ Remove firebug_lite from options, if set 
  168. if ( ! isset ( $this->options['version'] ) || version_compare( $this->options['version'], '5.1.2', '<' ) ) { 
  169. if ( isset( $this->options['firebug_lite'] ) ) { 
  170. unset( $this->options['firebug_lite'] ); 
  171. // 5.2.8+ Add disabled dashboards option 
  172. if ( ! isset ( $this->options['dashboards_disabled'] ) || version_compare( $this->options['version'], '5.2.8', '>' ) ) { 
  173. $this->options['dashboards_disabled'] = 0; 
  174. // Check is API option already exists - if not add it 
  175. $yst_ga_api = get_option( 'yst_ga_api' ); 
  176. if ( $yst_ga_api === false ) { 
  177. add_option( 'yst_ga_api', array(), '', 'no' ); 
  178. // Fallback to make sure every default option has a value 
  179. $defaults = $this->default_ga_values(); 
  180. if ( is_array( $defaults ) ) { 
  181. foreach ( $defaults[ $this->option_prefix ] as $key => $value ) { 
  182. if ( ! isset( $this->options[ $key ] ) ) { 
  183. $this->options[ $key ] = $value; 
  184. // Set to the current version now that we've done all needed upgrades 
  185. $this->options['version'] = GAWP_VERSION; 
  186. $this->update_option( $this->options ); 
  187.  
  188. /** 
  189. * Set the default GA settings here 
  190. * @return array 
  191. */ 
  192. public function default_ga_values() { 
  193. $options = array( 
  194. $this->option_prefix => array( 
  195. 'analytics_profile' => null,  
  196. 'analytics_profile_code' => null,  
  197. 'manual_ua_code' => 0,  
  198. 'manual_ua_code_field' => null,  
  199. 'track_internal_as_outbound' => null,  
  200. 'track_internal_as_label' => null,  
  201. 'track_outbound' => 0,  
  202. 'anonymous_data' => 0,  
  203. 'enable_universal' => 1,  
  204. 'demographics' => 0,  
  205. 'ignore_users' => array( 'administrator', 'editor' ),  
  206. 'dashboards_disabled' => 0,  
  207. 'anonymize_ips' => 0,  
  208. 'track_download_as' => 'event',  
  209. 'extensions_of_files' => 'doc, exe, js, pdf, ppt, tgz, zip, xls',  
  210. 'track_full_url' => 'domain',  
  211. 'subdomain_tracking' => null,  
  212. 'tag_links_in_rss' => 0,  
  213. 'allow_anchor' => 0,  
  214. 'add_allow_linker' => 0,  
  215. 'enhanced_link_attribution' => 0,  
  216. 'custom_code' => null,  
  217. 'debug_mode' => 0,  
  218. ); 
  219. $options = apply_filters( 'yst_ga_default-ga-values', $options, $this->option_prefix ); 
  220.  
  221. return $options; 
  222.  
  223. /** 
  224. * Load plugin textdomain 
  225. */ 
  226. public static function load_textdomain() { 
  227. load_plugin_textdomain( 'google-analytics-for-wordpress', false, dirname( plugin_basename( GAWP_FILE ) ) . '/languages/' ); 
  228.