Нет описания

Template.php 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. /*******************************************************************************
  3. * Copyright (c) 2019, Code Atlantic LLC
  4. ******************************************************************************/
  5. if ( ! defined( 'ABSPATH' ) ) {
  6. exit;
  7. }
  8. /**
  9. * Class PUM_Utils_Template
  10. */
  11. class PUM_Utils_Template {
  12. /**
  13. * @return array
  14. */
  15. public static function paths() {
  16. $template_dir = apply_filters( 'pum_template_path', 'popup-maker' );
  17. $old_template_dir = apply_filters( 'popmake_templates_dir', 'popmake_templates' );
  18. $file_paths = apply_filters( 'pum_template_paths', array(
  19. 1 => trailingslashit( get_stylesheet_directory() ) . $template_dir,
  20. 2 => trailingslashit( get_stylesheet_directory() ) . $old_template_dir,
  21. 10 => trailingslashit( get_template_directory() ) . $template_dir,
  22. 11 => trailingslashit( get_template_directory() ) . $old_template_dir,
  23. 100 => Popup_Maker::$DIR . 'templates',
  24. ) );
  25. /* @deprecated 1.8.9 */
  26. $file_paths = apply_filters( 'popmake_template_paths', $file_paths );
  27. // sort the file paths based on priority
  28. ksort( $file_paths, SORT_NUMERIC );
  29. return array_map( 'trailingslashit', $file_paths );
  30. }
  31. /**
  32. * Locate a template and return the path for inclusion.
  33. *
  34. * This is the load order:
  35. *
  36. * yourtheme / $template_path / $template_name
  37. * yourtheme / $template_name
  38. * $default_path / $template_name
  39. *
  40. * @access public
  41. *
  42. * @param string|array $template_names
  43. * @param bool $load
  44. * @param bool $require_once
  45. *
  46. * @return string
  47. * @internal param string $template_path (default: '')
  48. * @internal param string $default_path (default: '')
  49. *
  50. */
  51. public static function locate( $template_names, $load = false, $require_once = true ) {
  52. // No file found yet
  53. $located = false;
  54. $template_name = '';
  55. // Try to find a template file
  56. foreach ( (array) $template_names as $template_name ) {
  57. // Continue if template is empty
  58. if ( empty( $template_name ) ) {
  59. continue;
  60. }
  61. // Trim off any slashes from the template name
  62. $template_name = ltrim( $template_name, '/' );
  63. // try locating this template file by looping through the template paths
  64. foreach ( self::paths() as $template_path ) {
  65. if ( file_exists( $template_path . $template_name ) ) {
  66. $located = $template_path . $template_name;
  67. break;
  68. }
  69. }
  70. if ( $located ) {
  71. break;
  72. }
  73. }
  74. // Return what we found
  75. $located = apply_filters( 'pum_locate_template', $located, $template_name );
  76. if ( ( true == $load ) && ! empty( $located ) ) {
  77. load_template( $located, $require_once );
  78. }
  79. return $located;
  80. }
  81. /**
  82. * Locate a template part (for templates like the topic-loops).
  83. *
  84. * Popup_Maker::$DEBUG will prevent overrides in themes from taking priority.
  85. *
  86. * @param mixed $slug
  87. * @param string|bool $name (default: false)
  88. * @param bool $load
  89. *
  90. * @return string
  91. */
  92. public static function locate_part( $slug, $name = null, $load = false ) {
  93. $templates = array();
  94. if ( $name ) {
  95. // slug-name.php
  96. $templates[] = "{$slug}-{$name}.php";
  97. }
  98. // slug.php
  99. $templates[] = "{$slug}.php";
  100. // Allow template parts to be filtered
  101. $templates = apply_filters( 'pum_locate_template_part', $templates, $slug, $name );
  102. /* @deprecated 1.8.0 */
  103. $templates = apply_filters( 'popmake_get_template_part', $templates, $slug, $name );
  104. // Return the part that is found
  105. return self::locate( $templates, $load, false );
  106. }
  107. /**
  108. * Render file with extracted arguments.
  109. *
  110. * @param $template
  111. * @param array $args
  112. */
  113. public static function render( $template, $args = array() ) {
  114. if ( ! $template || ! file_exists( $template ) ) {
  115. _doing_it_wrong( __FUNCTION__, sprintf( '<code>%s</code> does not exist.', $template ), '1.0.0' );
  116. return;
  117. }
  118. if ( $args && is_array( $args ) ) {
  119. extract( $args );
  120. }
  121. include $template;
  122. }
  123. /**
  124. * Render a template part in $slug-$name.php fashion.
  125. *
  126. * Allows passing arguments that will be globally accessible in the template.
  127. *
  128. * @param string $slug
  129. * @param string $name
  130. * @param array $args
  131. */
  132. public static function part( $slug, $name = null, $args = array() ) {
  133. echo self::get_part( $slug, $name, $args );
  134. }
  135. /**
  136. * Get a template part in $slug-$name.php fashion.
  137. *
  138. * Allows passing arguments that will be globally accessible in the template.
  139. *
  140. * @param string $slug
  141. * @param string $name
  142. * @param array $args
  143. *
  144. * @return string
  145. */
  146. public static function get_part( $slug, $name = null, $args = array() ) {
  147. $template = self::locate_part( $slug, $name );
  148. ob_start();
  149. do_action( 'pum_before_template_part', $template, $slug, $name, $args );
  150. /* @deprecated 1.8.0 */
  151. do_action( 'get_template_part_' . $slug, $slug, $name );
  152. self::render( $template, $args );
  153. do_action( 'pum_after_template_part', $template, $slug, $name, $args );
  154. return ob_get_clean();
  155. }
  156. /**
  157. * Gets the rendered contents of the specified template file.
  158. *
  159. * @param $template_name
  160. * @param array $args
  161. *
  162. * @return string
  163. */
  164. public static function get( $template_name, $args = array() ) {
  165. $template = self::locate( $template_name );
  166. // Allow 3rd party plugin filter template file from their plugin.
  167. $template = apply_filters( 'pum_get_template', $template, $template_name, $args );
  168. ob_start();
  169. do_action( 'pum_before_template', $template_name, $template, $args );
  170. self::render( $template, $args );
  171. do_action( 'pum_after_template', $template_name, $template, $args );
  172. return ob_get_clean();
  173. }
  174. /**
  175. * Get other templates (e.g. popup content) passing attributes and including the file.
  176. *
  177. * @deprecated public
  178. *
  179. * @param string $template_name Template file name with extension: file-name.php
  180. * @param array $args (default: array())
  181. */
  182. public static function load( $template_name, $args = array() ) {
  183. echo self::get( $template_name, $args );
  184. }
  185. }