/wp-content/themes/genesis/lib/functions/options.php

https://github.com/livinglab/openlab · PHP · 316 lines · 113 code · 54 blank · 149 comment · 31 complexity · 7d9be06e0b6d278932e5efa73a7edbb0 MD5 · raw file

  1. <?php
  2. /**
  3. * Genesis Framework.
  4. *
  5. * WARNING: This file is part of the core Genesis Framework. DO NOT edit this file under any circumstances.
  6. * Please do all modifications in the form of a child theme.
  7. *
  8. * @package Genesis\Options
  9. * @author StudioPress
  10. * @license GPL-2.0-or-later
  11. * @link https://my.studiopress.com/themes/genesis/
  12. */
  13. /**
  14. * Return option from the options table and cache result.
  15. *
  16. * Applies `genesis_pre_get_option_$key` and `genesis_options` filters.
  17. *
  18. * Values pulled from the database are cached on each request, so a second request for the same value won't cause a
  19. * second DB interaction.
  20. *
  21. * @since 1.0.0
  22. *
  23. * @param string $key Option name.
  24. * @param string $setting Optional. Settings field name. Eventually defaults to `GENESIS_SETTINGS_FIELD` if not
  25. * passed as an argument.
  26. * @param bool $use_cache Optional. Whether to use the Genesis cache value or not. Default is true.
  27. * @return mixed The value of the `$key` in the database, or the return from
  28. * `genesis_pre_get_option_{$key}` short circuit filter if not `null`.
  29. */
  30. function genesis_get_option( $key, $setting = null, $use_cache = true ) {
  31. // The default is set here, so it doesn't have to be repeated in the function arguments for genesis_option() too.
  32. $setting = $setting ? $setting : GENESIS_SETTINGS_FIELD;
  33. // Allow child theme to short circuit this function.
  34. $pre = apply_filters( "genesis_pre_get_option_{$key}", null, $setting );
  35. if ( null !== $pre ) {
  36. return $pre;
  37. }
  38. // Bypass cache if viewing site in Customizer.
  39. if ( genesis_is_customizer() ) {
  40. $use_cache = false;
  41. }
  42. // If we need to bypass the cache.
  43. if ( ! $use_cache ) {
  44. $options = get_option( $setting );
  45. if ( ! is_array( $options ) || ! array_key_exists( $key, $options ) ) {
  46. return '';
  47. }
  48. return is_array( $options[ $key ] ) ? $options[ $key ] : wp_kses_decode_entities( $options[ $key ] );
  49. }
  50. // Setup caches.
  51. static $settings_cache = array();
  52. static $options_cache = array();
  53. // Check options cache.
  54. if ( isset( $options_cache[ $setting ][ $key ] ) ) {
  55. // Option has been cached.
  56. return $options_cache[ $setting ][ $key ];
  57. }
  58. // Check settings cache.
  59. if ( isset( $settings_cache[ $setting ] ) ) {
  60. // Setting has been cached.
  61. $options = apply_filters( 'genesis_options', $settings_cache[ $setting ], $setting );
  62. } else {
  63. // Set value and cache setting.
  64. $settings_cache[ $setting ] = apply_filters( 'genesis_options', get_option( $setting ), $setting );
  65. $options = $settings_cache[ $setting ];
  66. }
  67. // Check for non-existent option.
  68. if ( ! is_array( $options ) || ! array_key_exists( $key, (array) $options ) ) {
  69. // Cache non-existent option.
  70. $options_cache[ $setting ][ $key ] = '';
  71. } else {
  72. // Option has not previously been cached, so cache now.
  73. $options_cache[ $setting ][ $key ] = is_array( $options[ $key ] ) ? $options[ $key ] : wp_kses_decode_entities( $options[ $key ] );
  74. }
  75. return $options_cache[ $setting ][ $key ];
  76. }
  77. /**
  78. * Echo options from the options database.
  79. *
  80. * @since 1.0.0
  81. *
  82. * @param string $key Option name.
  83. * @param string $setting Optional. Settings field name. Eventually defaults to GENESIS_SETTINGS_FIELD.
  84. * @param bool $use_cache Optional. Whether to use the Genesis cache value or not. Default is true.
  85. */
  86. function genesis_option( $key, $setting = null, $use_cache = true ) {
  87. echo genesis_get_option( $key, $setting, $use_cache );
  88. }
  89. /**
  90. * Return SEO options from the SEO options database.
  91. *
  92. * @since 1.0.0
  93. *
  94. * @param string $key Option name.
  95. * @param bool $use_cache Optional. Whether to use the Genesis cache value or not. Defaults to true.
  96. * @return mixed The value of the `$key` in the database, or the return from
  97. * `genesis_pre_get_option_{$key}` short circuit filter if not `null`.
  98. */
  99. function genesis_get_seo_option( $key, $use_cache = true ) {
  100. return genesis_get_option( $key, GENESIS_SEO_SETTINGS_FIELD, $use_cache );
  101. }
  102. /**
  103. * Echo an SEO option from the SEO options database.
  104. *
  105. * @since 1.0.0
  106. *
  107. * @param string $key Option name.
  108. * @param bool $use_cache Optional. Whether to use the Genesis cache value or not. Defaults to true.
  109. */
  110. function genesis_seo_option( $key, $use_cache = true ) {
  111. genesis_option( $key, GENESIS_SEO_SETTINGS_FIELD, $use_cache );
  112. }
  113. /**
  114. * Return a CPT Archive setting value from the options table.
  115. *
  116. * @since 2.0.0
  117. *
  118. * @param string $key Option name.
  119. * @param string $post_type_name Post type name.
  120. * @param bool $use_cache Optional. Whether to use the Genesis cache value or not. Defaults to true.
  121. * @return mixed The value of the `$key` in the database, or the return from
  122. * `genesis_pre_get_option_{$key}` short circuit filter if not `null`.
  123. */
  124. function genesis_get_cpt_option( $key, $post_type_name = '', $use_cache = true ) {
  125. $post_type_name = genesis_get_global_post_type_name( $post_type_name );
  126. return genesis_get_option( $key, GENESIS_CPT_ARCHIVE_SETTINGS_FIELD_PREFIX . $post_type_name, $use_cache );
  127. }
  128. /**
  129. * Echo a CPT Archive option from the options table.
  130. *
  131. * @since 2.0.0
  132. *
  133. * @param string $key Option name.
  134. * @param string $post_type_name Post type name.
  135. * @param bool $use_cache Optional. Whether to use the Genesis cache value or not. Defaults to true.
  136. */
  137. function genesis_cpt_option( $key, $post_type_name, $use_cache = true ) {
  138. echo genesis_get_cpt_option( $key, $post_type_name, $use_cache );
  139. }
  140. /**
  141. * Echo data from a post or page custom field.
  142. *
  143. * Echo only the first value of custom field.
  144. *
  145. * Pass in a `printf()` pattern as the second parameter and have that wrap around the value, if the value is not falsy.
  146. *
  147. * @since 1.0.0
  148. *
  149. * @param string $field Custom field key.
  150. * @param string $output_pattern `printf()` compatible output pattern.
  151. * @param int $post_id Optional. Post ID to use for Post Meta lookup, defaults to `get_the_ID()`.
  152. */
  153. function genesis_custom_field( $field, $output_pattern = '%s', $post_id = null ) {
  154. $value = genesis_get_custom_field( $field, $post_id );
  155. if ( $value ) {
  156. printf( $output_pattern, $value );
  157. }
  158. }
  159. /**
  160. * Return custom field post meta data.
  161. *
  162. * Return only the first value of custom field. Return empty string if field is blank or not set.
  163. *
  164. * @since 1.0.0
  165. *
  166. * @param string $field Custom field key.
  167. * @param int $post_id Optional. Post ID to use for Post Meta lookup, defaults to `get_the_ID()`.
  168. * @return string|bool Return value or empty string on failure.
  169. */
  170. function genesis_get_custom_field( $field, $post_id = null ) {
  171. // Use get_the_ID() if no $post_id is specified.
  172. $post_id = empty( $post_id ) ? get_the_ID() : $post_id;
  173. if ( ! $post_id ) {
  174. return '';
  175. }
  176. $custom_field = get_post_meta( $post_id, $field, true );
  177. if ( ! $custom_field ) {
  178. return '';
  179. }
  180. return is_array( $custom_field ) ? $custom_field : wp_kses_decode_entities( $custom_field );
  181. }
  182. /**
  183. * Save post meta / custom field data for a post or page.
  184. *
  185. * It verifies the nonce, then checks we're not doing autosave, ajax or a future post request. It then checks the
  186. * current user's permissions, before finally* either updating the post meta, or deleting the field if the value was not
  187. * truthy.
  188. *
  189. * By passing an array of fields => values from the same meta box (and therefore same nonce) into the $data argument,
  190. * repeated checks against the nonce, request and permissions are avoided.
  191. *
  192. * @since 1.9.0
  193. *
  194. * @param array $data Key/Value pairs of data to save in '_field_name' => 'value' format.
  195. * @param string $nonce_action Nonce action for use with wp_verify_nonce().
  196. * @param string $nonce_name Name of the nonce to check for permissions.
  197. * @param WP_Post|int $post Post object or ID.
  198. * @param int $deprecated Deprecated (formerly accepted a post ID).
  199. * @return void Return early if permissions are incorrect, doing autosave, Ajax or future post.
  200. */
  201. function genesis_save_custom_fields( array $data, $nonce_action, $nonce_name, $post, $deprecated = null ) {
  202. if ( ! empty( $deprecated ) ) {
  203. _deprecated_argument( __FUNCTION__, '2.0.0' );
  204. }
  205. // Verify the nonce.
  206. if ( ! isset( $_POST[ $nonce_name ] ) || ! wp_verify_nonce( $_POST[ $nonce_name ], $nonce_action ) ) {
  207. return;
  208. }
  209. // Don't try to save the data under autosave, ajax, or future post.
  210. if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
  211. return;
  212. }
  213. if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
  214. return;
  215. }
  216. if ( defined( 'DOING_CRON' ) && DOING_CRON ) {
  217. return;
  218. }
  219. // Grab the post object.
  220. if ( null !== $deprecated ) {
  221. $post = get_post( $deprecated );
  222. } else {
  223. $post = get_post( $post );
  224. }
  225. // Don't save if WP is creating a revision (same as DOING_AUTOSAVE?).
  226. if ( 'revision' === get_post_type( $post ) ) {
  227. return;
  228. }
  229. // Check that the user is allowed to edit the post.
  230. if ( ! current_user_can( 'edit_post', $post->ID ) ) {
  231. return;
  232. }
  233. // Cycle through $data, insert value or delete field.
  234. foreach ( (array) $data as $field => $value ) {
  235. // Save $value, or delete if the $value is empty.
  236. if ( $value ) {
  237. update_post_meta( $post->ID, $field, $value );
  238. } else {
  239. delete_post_meta( $post->ID, $field );
  240. }
  241. }
  242. }
  243. /**
  244. * Takes an array of new settings, merges them with the old settings, and pushes them into the database.
  245. *
  246. * @since 2.1.0
  247. *
  248. * @param string|array $new New settings. Can be a string, or an array.
  249. * @param string $setting Optional. Settings field name. Default is GENESIS_SETTINGS_FIELD.
  250. * @return bool `true` if option was updated, `false` otherwise.
  251. */
  252. function genesis_update_settings( $new = '', $setting = GENESIS_SETTINGS_FIELD ) {
  253. $old = get_option( $setting );
  254. $settings = wp_parse_args( $new, $old );
  255. // Allow settings to be deleted.
  256. foreach ( $settings as $key => $value ) {
  257. if ( 'unset' == $value ) {
  258. unset( $settings[ $key ] );
  259. }
  260. }
  261. return update_option( $setting, $settings );
  262. }