/infinity/plugins/infinite-scroll/includes/options.php

https://bitbucket.org/cevenson/photosites-themes · PHP · 191 lines · 72 code · 51 blank · 68 comment · 6 complexity · 17a1a5606313b8d515c4468a5d2fcd27 MD5 · raw file

  1. <?php
  2. /**
  3. * Provides interface to store and retrieve plugin and user options
  4. * @subpackage Infinite_Scroll_Options
  5. * @package Infinite_Scroll
  6. */
  7. class Infinite_Scroll_Options {
  8. //default scope for options when called directly,
  9. //choices: site, user, or global (user option across sites)
  10. public $defaults = array();
  11. private $parent;
  12. /**
  13. * Stores parent class as static
  14. * @param class $parent (reference) the parent class
  15. */
  16. function __construct( &$parent ) {
  17. $this->parent = &$parent;
  18. add_action( 'admin_init', array( &$this, 'options_init' ) );
  19. add_filter( $this->parent->prefix . 'options', array( &$this, 'default_options_filter' ), 20 );
  20. add_filter( $this->parent->prefix . 'js_options', array( &$this, 'db_version_filter' ) );
  21. }
  22. /**
  23. * Tells WP that we're using a custom settings field
  24. */
  25. function options_init() {
  26. register_setting( $this->parent->slug_, $this->parent->slug_, array( &$this, 'validate' ) );
  27. }
  28. /**
  29. * Runs options through filter prior to saving
  30. * @param array $options the options array
  31. * @return array sanitized options array
  32. */
  33. function validate( $options ) {
  34. //add slashes to JS selectors
  35. $js = array ( 'nextSelector', 'navSelector', 'itemSelector', 'contentSelector' );
  36. foreach ( $js as $field ) {
  37. if ( !isset( $options[$field] ) )
  38. continue;
  39. $options[$field] = addslashes( $options[ $field ] );
  40. }
  41. //force post-style kses on messages
  42. foreach ( array( 'finishedMsg', 'msgText' ) as $field ) {
  43. if ( !isset( $options['loading'][$field] ) )
  44. continue;
  45. // wp_filter_post_kses will add slashes to something like "you've" -> "you\'ve" but not added slashes to other slashes
  46. // Escaping the slashes and then stripping them, gets past this problem and allows preservation of intentionally inserted slashes
  47. $options['loading'][$field] = stripslashes(wp_filter_post_kses( addslashes($options['loading'][$field] )));
  48. }
  49. //handle image resets
  50. if ( isset( $_POST[ 'reset_default_image'] ) )
  51. $options["loading"]['img'] = $this->defaults["loading"]['img'];
  52. //pull existing image if none is given
  53. if ( empty( $options["loading"]['img'] ) )
  54. $options["loading"]['img'] = $this->loading["img"];
  55. // force `debug` to be a bool
  56. $options["debug"] = (bool)$options["debug"];
  57. return apply_filters( $this->parent->prefix . 'options_validate', $options );
  58. }
  59. /**
  60. * Allows overloading to get option value
  61. * Usage: $value = $object->{option name}
  62. * @param string $name the option name
  63. * @return mixed the option value
  64. */
  65. function __get( $name ) {
  66. return $this->get_option( $name );
  67. }
  68. /**
  69. * Allows overloading to set option value
  70. * Usage: $object->{option name} = $value
  71. * @param string $name unique option key
  72. * @param mixed $value the value to store
  73. * @return bool success/fail
  74. */
  75. function __set( $name, $value ) {
  76. return $this->set_option( $name, $value );
  77. }
  78. /**
  79. * Retreive the options array
  80. * @return array the options
  81. */
  82. function get_options( ) {
  83. if ( !$options = wp_cache_get( 'options', $this->parent->slug ) ) {
  84. $options = get_option( $this->parent->slug_ );
  85. wp_cache_set( 'options', $options, $this->parent->slug );
  86. }
  87. return apply_filters( $this->parent->prefix . 'options', $options );
  88. }
  89. /**
  90. * If any options are not set, merge with defaults
  91. * @param array $options the saved options
  92. * @return array the merged options with defaults
  93. */
  94. function default_options_filter( $options ) {
  95. $options = wp_parse_args( $options, $this->defaults );
  96. wp_cache_set( 'options', $options, $this->parent->slug );
  97. return $options;
  98. }
  99. /**
  100. * Retreives a specific option
  101. * @param string $option the unique option key
  102. * @return mixed the value
  103. */
  104. function get_option( $option ) {
  105. $options = $this->get_options( );
  106. $value = ( isset( $options[ $option ] ) ) ? $options[ $option ] : false;
  107. return apply_filters( $this->parent->prefix . $option, $value );
  108. }
  109. /**
  110. * Sets a specific option
  111. * @return bool success/fail
  112. * @param string $key the unique option key
  113. * @param mixed $value the value
  114. */
  115. function set_option( $key, $value ) {
  116. $options = array( $key => $value );
  117. $this->set_options( $options );
  118. }
  119. /**
  120. * Sets all plugin options
  121. * @param array $options the options array
  122. * @param bool $merge (optional) whether or not to merge options arrays or overwrite
  123. * @return bool success/fail
  124. */
  125. function set_options( $options, $merge = true ) {
  126. if ( $merge ) {
  127. $defaults = $this->get_options();
  128. $options = wp_parse_args( $options, $defaults );
  129. }
  130. wp_cache_set( 'options', $options, $this->parent->slug );
  131. return update_option( $this->parent->slug_, $options );
  132. }
  133. /**
  134. * Don't output db_version to front end when passing args to javascript function
  135. */
  136. function db_version_filter( $options ) {
  137. unset( $options['db_version'] );
  138. return $options;
  139. }
  140. }