PageRenderTime 27ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/themes/news/library/functions/core.php

https://gitlab.com/puppet-br/puppet-br.org-wordpress
PHP | 272 lines | 83 code | 42 blank | 147 comment | 16 complexity | 745c9c3a68be79a166b15d6e69f78b4f MD5 | raw file
  1. <?php
  2. /**
  3. * The core functions file for the Hybrid framework. Functions defined here are generally
  4. * used across the entire framework to make various tasks faster. This file should be loaded
  5. * prior to any other files because its functions are needed to run the framework.
  6. *
  7. * @package HybridCore
  8. * @subpackage Functions
  9. */
  10. /**
  11. * Defines the theme prefix. This allows developers to infinitely change the theme. In theory,
  12. * one could use the Hybrid core to create their own theme or filter 'hybrid_prefix' with a
  13. * plugin to make it easier to use hooks across multiple themes without having to figure out
  14. * each theme's hooks (assuming other themes used the same system).
  15. *
  16. * @since 0.7.0
  17. * @uses get_template() Defines the theme prefix based on the theme directory.
  18. * @global object $hybrid The global Hybrid object.
  19. * @return string $hybrid->prefix The prefix of the theme.
  20. */
  21. function hybrid_get_prefix() {
  22. global $hybrid;
  23. /* If the global prefix isn't set, define it. Plugin/theme authors may also define a custom prefix. */
  24. if ( empty( $hybrid->prefix ) )
  25. $hybrid->prefix = sanitize_key( apply_filters( 'hybrid_prefix', get_template() ) );
  26. return $hybrid->prefix;
  27. }
  28. /**
  29. * Defines the theme textdomain. This allows the framework to recognize the proper textdomain of the
  30. * parent theme. Theme developers building from the framework should use this function in their templates
  31. * to easily define the correct textdomain.
  32. *
  33. * @since 0.7.0
  34. * @uses get_template() Defines the theme textdomain based on the template directory.
  35. * @global object $hybrid The global Hybrid object.
  36. * @return string $hybrid->textdomain The textdomain of the theme.
  37. */
  38. function hybrid_get_textdomain() {
  39. global $hybrid;
  40. /* If the global textdomain isn't set, define it. Plugin/theme authors may also define a custom textdomain. */
  41. if ( empty( $hybrid->textdomain ) )
  42. $hybrid->textdomain = sanitize_key( apply_filters( hybrid_get_prefix() . '_textdomain', get_template() ) );
  43. return $hybrid->textdomain;
  44. }
  45. /**
  46. * Returns the textdomain for the child theme.
  47. *
  48. * @since 1.2.0
  49. * @uses get_stylesheet() Defines the child theme textdomain based on the stylesheet directory.
  50. * @global object $hybrid The global Hybrid object.
  51. * @return string $hybrid->child_theme_textdomain The textdomain of the child theme.
  52. */
  53. function hybrid_get_child_textdomain() {
  54. global $hybrid;
  55. /* If a child theme isn't active, return an empty string. */
  56. if ( !is_child_theme() )
  57. return '';
  58. /* If the global textdomain isn't set, define it. Plugin/theme authors may also define a custom textdomain. */
  59. if ( empty( $hybrid->child_textdomain ) )
  60. $hybrid->child_textdomain = sanitize_key( apply_filters( hybrid_get_prefix() . '_child_textdomain', get_stylesheet() ) );
  61. return $hybrid->child_textdomain;
  62. }
  63. /**
  64. * Filters the 'load_textdomain_mofile' filter hook so that we can change the directory and file name
  65. * of the mofile for translations. This allows child themes to have a folder called /languages with translations
  66. * of their parent theme so that the translations aren't lost on a parent theme upgrade.
  67. *
  68. * @since 0.9.0
  69. * @param string $mofile File name of the .mo file.
  70. * @param string $domain The textdomain currently being filtered.
  71. */
  72. function hybrid_load_textdomain( $mofile, $domain ) {
  73. /* If the $domain is for the parent or child theme, search for a $domain-$locale.mo file. */
  74. if ( $domain == hybrid_get_textdomain() || $domain == hybrid_get_child_textdomain() ) {
  75. /* Check for a $domain-$locale.mo file in the parent and child theme root and /languages folder. */
  76. $locale = get_locale();
  77. $locate_mofile = locate_template( array( "languages/{$domain}-{$locale}.mo", "{$domain}-{$locale}.mo" ) );
  78. /* If a mofile was found based on the given format, set $mofile to that file name. */
  79. if ( !empty( $locate_mofile ) )
  80. $mofile = $locate_mofile;
  81. }
  82. /* Return the $mofile string. */
  83. return $mofile;
  84. }
  85. /**
  86. * Adds contextual action hooks to the theme. This allows users to easily add context-based content
  87. * without having to know how to use WordPress conditional tags. The theme handles the logic.
  88. *
  89. * An example of a basic hook would be 'hybrid_header'. The do_atomic() function extends that to
  90. * give extra hooks such as 'hybrid_singular_header', 'hybrid_singular-post_header', and
  91. * 'hybrid_singular-post-ID_header'.
  92. *
  93. * @since 0.7.0
  94. * @uses hybrid_get_prefix() Gets the theme prefix.
  95. * @uses hybrid_get_context() Gets the context of the current page.
  96. * @param string $tag Usually the location of the hook but defines what the base hook is.
  97. * @param mixed $arg,... Optional additional arguments which are passed on to the functions hooked to the action.
  98. */
  99. function do_atomic( $tag = '', $arg = '' ) {
  100. if ( empty( $tag ) )
  101. return false;
  102. /* Get the theme prefix. */
  103. $pre = hybrid_get_prefix();
  104. /* Get the args passed into the function and remove $tag. */
  105. $args = func_get_args();
  106. array_splice( $args, 0, 1 );
  107. /* Do actions on the basic hook. */
  108. do_action_ref_array( "{$pre}_{$tag}", $args );
  109. /* Loop through context array and fire actions on a contextual scale. */
  110. foreach ( (array)hybrid_get_context() as $context )
  111. do_action_ref_array( "{$pre}_{$context}_{$tag}", $args );
  112. }
  113. /**
  114. * Adds contextual filter hooks to the theme. This allows users to easily filter context-based content
  115. * without having to know how to use WordPress conditional tags. The theme handles the logic.
  116. *
  117. * An example of a basic hook would be 'hybrid_entry_meta'. The apply_atomic() function extends
  118. * that to give extra hooks such as 'hybrid_singular_entry_meta', 'hybrid_singular-post_entry_meta',
  119. * and 'hybrid_singular-post-ID_entry_meta'.
  120. *
  121. * @since 0.7.0
  122. * @uses hybrid_get_prefix() Gets the theme prefix.
  123. * @uses hybrid_get_context() Gets the context of the current page.
  124. * @param string $tag Usually the location of the hook but defines what the base hook is.
  125. * @param mixed $value The value on which the filters hooked to $tag are applied on.
  126. * @param mixed $var,... Additional variables passed to the functions hooked to $tag.
  127. * @return mixed $value The value after it has been filtered.
  128. */
  129. function apply_atomic( $tag = '', $value = '' ) {
  130. if ( empty( $tag ) )
  131. return false;
  132. /* Get theme prefix. */
  133. $pre = hybrid_get_prefix();
  134. /* Get the args passed into the function and remove $tag. */
  135. $args = func_get_args();
  136. array_splice( $args, 0, 1 );
  137. /* Apply filters on the basic hook. */
  138. $value = $args[0] = apply_filters_ref_array( "{$pre}_{$tag}", $args );
  139. /* Loop through context array and apply filters on a contextual scale. */
  140. foreach ( (array)hybrid_get_context() as $context )
  141. $value = $args[0] = apply_filters_ref_array( "{$pre}_{$context}_{$tag}", $args );
  142. /* Return the final value once all filters have been applied. */
  143. return $value;
  144. }
  145. /**
  146. * Wraps the output of apply_atomic() in a call to do_shortcode(). This allows developers to use
  147. * context-aware functionality alongside shortcodes. Rather than adding a lot of code to the
  148. * function itself, developers can create individual functions to handle shortcodes.
  149. *
  150. * @since 0.7.0
  151. * @param string $tag Usually the location of the hook but defines what the base hook is.
  152. * @param mixed $value The value to be filtered.
  153. * @return mixed $value The value after it has been filtered.
  154. */
  155. function apply_atomic_shortcode( $tag = '', $value = '' ) {
  156. return do_shortcode( apply_atomic( $tag, $value ) );
  157. }
  158. /**
  159. * The theme can save multiple things in a transient to help speed up page load times. We're
  160. * setting a default of 12 hours or 43,200 seconds (60 * 60 * 12).
  161. *
  162. * @since 0.8.0
  163. * @return int Transient expiration time in seconds.
  164. */
  165. function hybrid_get_transient_expiration() {
  166. return apply_filters( hybrid_get_prefix() . '_transient_expiration', 43200 );
  167. }
  168. /**
  169. * Function for formatting a hook name if needed. It automatically adds the theme's prefix to
  170. * the hook, and it will add a context (or any variable) if it's given.
  171. *
  172. * @since 0.7.0
  173. * @param string $tag The basic name of the hook (e.g., 'before_header').
  174. * @param string $context A specific context/value to be added to the hook.
  175. */
  176. function hybrid_format_hook( $tag, $context = '' ) {
  177. return hybrid_get_prefix() . ( ( !empty( $context ) ) ? "_{$context}" : "" ). "_{$tag}";
  178. }
  179. /**
  180. * Function for setting the content width of a theme. This does not check if a content width has been set; it
  181. * simply overwrites whatever the content width is.
  182. *
  183. * @since 1.2.0
  184. * @global int $content_width The width for the theme's content area.
  185. * @param int $width Numeric value of the width to set.
  186. */
  187. function hybrid_set_content_width( $width = '' ) {
  188. global $content_width;
  189. $content_width = absint( $width );
  190. }
  191. /**
  192. * Function for getting the theme's content width.
  193. *
  194. * @since 1.2.0
  195. * @global int $content_width The width for the theme's content area.
  196. * @return int $content_width
  197. */
  198. function hybrid_get_content_width() {
  199. global $content_width;
  200. return $content_width;
  201. }
  202. /**
  203. * Gets theme data and stores it in the global $hybrid variable. By storing it, it can be accessed quickly without
  204. * having to run through the get_theme_data() function again.
  205. *
  206. * @since 1.2.0
  207. * @param string $path Whether to use the template (parent theme) or stylesheet (child theme) path.
  208. */
  209. function hybrid_get_theme_data( $path = 'template' ) {
  210. global $hybrid;
  211. /* If 'template' is requested, get the parent theme data. */
  212. if ( 'template' == $path ) {
  213. /* If the parent theme data isn't set, grab it with the get_theme_data() function. */
  214. if ( empty( $hybrid->theme_data ) )
  215. $hybrid->theme_data = get_theme_data( trailingslashit( TEMPLATEPATH ) . 'style.css' );
  216. /* Return the parent theme data. */
  217. return $hybrid->theme_data;
  218. }
  219. /* If 'stylesheet' is requested, get the child theme data. */
  220. elseif ( 'stylesheet' == $path ) {
  221. /* If the child theme data isn't set, grab it with the get_theme_data() function. */
  222. if ( empty( $hybrid->child_theme_data ) )
  223. $hybrid->child_theme_data = get_theme_data( trailingslashit( STYLESHEETPATH ) . 'style.css' );
  224. /* Return the child theme data. */
  225. return $hybrid->child_theme_data;
  226. }
  227. /* Return false for everything else. */
  228. return false;
  229. }
  230. ?>