PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/opentickets-community-edition/inc/sys/templates.php

https://gitlab.com/leobelizquierdo/cabotsubmitter-wordpress
PHP | 275 lines | 157 code | 56 blank | 62 comment | 35 complexity | 31d2cd9b8e7c7612b502a4629302e34b MD5 | raw file
  1. <?php if ( __FILE__ == $_SERVER['SCRIPT_FILENAME'] ) die( header( 'Location: /') );
  2. if (!class_exists('QSOT_Templates')):
  3. class QSOT_Templates {
  4. protected static $o = null; // holder for all options of the events plugin
  5. public static function pre_init() {
  6. // load the settings. theya re required for everything past this point
  7. $settings_class_name = apply_filters('qsot-settings-class-name', '');
  8. if (empty($settings_class_name)) return;
  9. self::$o = call_user_func_array(array($settings_class_name, "instance"), array());
  10. // qsot template locator. checks theme first, then our templates dir
  11. add_filter('qsot-locate-template', array(__CLASS__, 'locate_template'), 10, 4);
  12. // similar to above, only specifically for templates that we may have overriden from woo.... like admin templates
  13. add_filter('woocommerce_locate_template', array(__CLASS__, 'wc_locate_template'), 10, 3);
  14. // add our plugin page templates to the list of acceptable page templates
  15. add_filter( 'init', array( __CLASS__, 'rig_theme_page_template_cache' ), 0 );
  16. // add our templates to the list of templates to show in the admin, which is crosschecked with the list of acceptable page templates
  17. add_filter( 'theme_page_templates', array( __CLASS__, 'add_extra_templates' ), 10, 1 );
  18. // load our plugin page templates when they are needed, since core does not allow doing this from a plugin dir itself
  19. add_filter( 'template_include', array( __CLASS__, 'intercept_page_template_request' ), 100, 1 );
  20. }
  21. // we need to add our plugin's callendar template to the theme cache of page templates, because for some dumb reason, core started requiring that a page template exist in the theme in order to appear on the list.
  22. // this function allows us to use our page template that is packaged with the plugin, regardless of whether it lives in the theme or not
  23. // this is needed because if you look in WP_Theme::get_page_templates(), there is no way to inject a page template if there is not one in the theme directory
  24. public static function rig_theme_page_template_cache() {
  25. // get a list of page templates not in the theme
  26. $extras = apply_filters('qsot-templates-page-templates', array());
  27. // load the exisitng list of templates for the theme. start by loading the theme
  28. $theme = wp_get_theme();
  29. // build the cache hash, used for the cache keys. copied from WP_Theme::__construct()
  30. $cache_hash = md5( $theme->get_theme_root() . '/' . $theme->get_stylesheet() );
  31. // force the cache key to generate, in case it hasn't already
  32. $theme->get_page_templates();
  33. // fetch the current list of templates. copied from WP_Theme::cache_get
  34. $list = wp_cache_get( 'page_templates-' . $cache_hash, 'themes' );
  35. // add our list to the original list
  36. $list = array_merge( is_array( $list ) ? $list : array(), $extras );
  37. // save the list again, once our page templates have been added. copied from WP_Theme::cache_add
  38. wp_cache_set( 'page_templates-' . $cache_hash, $list, 'themes', 1800 );
  39. // profit!
  40. }
  41. // add our page template to the list of page templates
  42. public static function add_extra_templates( $list ) {
  43. $extras = apply_filters( 'qsot-templates-page-templates', array() );
  44. return array_merge( $list, $extras );
  45. }
  46. // when a page template that existing in our plugin is requested, we need to load it from the plugin if it does not exist in the theme dir. this function handles that
  47. public static function intercept_page_template_request( $current ) {
  48. // only perform this logic if the current requested assset is a page
  49. if ( ! is_page() )
  50. return $current;
  51. // get a list of our plugin page templates
  52. $intercept = apply_filters( 'qsot-templates-page-templates', array() );
  53. // find the name of the template requested by this page
  54. $template = get_page_template_slug();
  55. // if the template is on the list of templates inside our plugin, then
  56. if ( isset( $intercept[ $template ] ) ) {
  57. $templates = array();
  58. // add our file to a list of files to search for in the plugin template dir
  59. if ( $template && 0 === validate_file( $template ) )
  60. $templates[] = $template;
  61. // find any files that match the filename in the stylesheet dir, then the theme dir, then our plugin dir. if none are found, then use whatever the $current was when the function was called
  62. $current = apply_filters( 'qsot-locate-template', $current, $templates );
  63. }
  64. return $current;
  65. }
  66. // locate a given template. first check the theme for it, then our plugin dirs for fallbacks
  67. public static function locate_template( $current='', $files=array(), $load=false, $require_once=false ) {
  68. // normalize the list of potential files
  69. $files = ! empty( $files ) ? (array)$files : $files;
  70. // if we have a list of files
  71. if ( is_array( $files ) && count( $files ) ) {
  72. // first search the theme
  73. $templ = locate_template( $files, $load, $require_once );
  74. // if there was not a matching file in the theme, then search our backup dirs
  75. if ( empty( $templ ) ) {
  76. // aggregate a list of backup dirs to search
  77. $dirs = apply_filters( 'qsot-template-dirs', array( self::$o->core_dir . 'templates/' ) );
  78. $qsot_path = '';
  79. // add the legacy directory within the theme that holds the legacy OTCE templates
  80. array_unshift( $dirs, get_stylesheet_directory() . '/' . $qsot_path, get_template_directory() . '/' . $qsot_path );
  81. // for each file in the list, try to find it in each backup dir
  82. foreach ( $files as $file ) {
  83. // normalize the filename, and skip any empty ones
  84. $file = trim( $file );
  85. if ( '' === $file )
  86. continue;
  87. // check each backup dir for this file
  88. foreach ( $dirs as $dir ) {
  89. $dir = trailingslashit( $dir );
  90. // if the file exists, then use that one, and bail the remainder of the search
  91. if ( file_exists( $dir . $file ) && is_readable( $dir . $file ) ) {
  92. $templ = $dir . $file;
  93. break 2;
  94. }
  95. }
  96. }
  97. }
  98. // if there is a template found, and we are being asked to include it, the include it, by either 'require' or 'include' depending on the passed params
  99. if ( ! empty( $templ ) && $load ) {
  100. if ( $require_once )
  101. require_once $templ;
  102. else
  103. include $templ;
  104. }
  105. // if we found a template, make sure to update the return value with the full path to the file
  106. if ( ! empty( $templ ) )
  107. $current = $templ;
  108. }
  109. return $current;
  110. }
  111. public static function wc_locate_template($current, $template_name, $template_path) {
  112. $name = $template_name;
  113. $found = self::locate_woo_template( $name );
  114. return $found ? $found : $current;
  115. }
  116. // created to track down a specific theme issue
  117. protected static function _lg( $msg ) {
  118. ?>
  119. <script>
  120. ( function() { var args = <?php echo @json_encode( func_get_args() ) ?>; if ( console && console.log && 'function' == typeof console.log ) console.log.apply( console.log, args ); } )();
  121. </script>
  122. <?php
  123. }
  124. // locate a template that comes with core woocommerce. this template could have a potential override template in this plugin
  125. public static function locate_woo_template( $name, $type='' ) {
  126. $found = null;
  127. $key = $type . ':' . md5( $name );
  128. // if we found the file previously, then just use the cache instead of continuously looking for it
  129. $cache = wp_cache_get( $key, 'qsot-wc-templates', false, $found );
  130. if ( ( isset( $found ) && $found ) || ( ! isset( $found ) && false !== $cache ) ) // account for memcached plugin
  131. return $cache;
  132. // see if the file is overridden in the theme. if it is, just use that
  133. $found = locate_template( array( $name ), false, false );
  134. if ( ! empty( $found ) ) {
  135. $found = apply_filters( 'qsot-woo-template', $found, $type, $name );
  136. wp_cache_set( $key, $found, 'qsot-wc-templates', 3600 );
  137. return $found;
  138. }
  139. // otherwise, try to find the appropriate file, by first checking our plugin, then checking WC core
  140. // find the core WC dir
  141. $woodir = trailingslashit( WC()->plugin_path() );
  142. // depending on the type of template we need, choose a specific subdir to check
  143. switch ( $type ) {
  144. case 'admin':
  145. $qsot_path = 'templates/admin/';
  146. $woo_path = 'includes/admin/';
  147. break;
  148. default:
  149. $qsot_path = 'templates/';
  150. $woo_path = 'templates/';
  151. break;
  152. }
  153. // construct a cascading list of dirs to check for the appropriate file in
  154. $dirs = apply_filters( 'qsot-template-dirs', array(
  155. get_stylesheet_directory() . '/woocommerce/',
  156. get_template_directory() . '/woocommerce/',
  157. get_stylesheet_directory() . '/templates/',
  158. get_template_directory() . '/templates/',
  159. QSOT::plugin_dir() . $qsot_path,
  160. $woodir . $woo_path,
  161. ), $qsot_path, $woo_path, 'woocommerce' );
  162. // force the theme to be the prime override, using our special subpath as a context
  163. array_unshift( $dirs, get_stylesheet_directory() . '/' . $qsot_path, get_template_directory() . '/' . $qsot_path );
  164. // cycle through the dir list, until we find the first matching file
  165. foreach ( $dirs as $dir ) {
  166. if ( file_exists( ( $file = trailingslashit( $dir ) . $name ) ) ) {
  167. $found = $file;
  168. break;
  169. }
  170. }
  171. // allow modification, and then store in cache
  172. $found = apply_filters( 'qsot-woo-template', $found, $type, $name );
  173. wp_cache_set( $key, $found, 'qsot-wc-templates', 3600 );
  174. return $found;
  175. }
  176. // include a template, and make specific $args local vars
  177. public static function include_template( $template, $args=array(), $echo__output=true, $lookup=true ) {
  178. // get the template from the template filename
  179. if ( $lookup )
  180. $template = apply_filters( 'qsot-locate-template', false, (array) $template, false, false );
  181. // extract args to local vars
  182. extract( $args );
  183. // capture the output
  184. ob_start();
  185. include $template;
  186. $out = ob_get_contents();
  187. ob_end_clean();
  188. // if we are being asked to print the output then do so now
  189. if ( $echo__output )
  190. echo $out;
  191. return trim( preg_replace( '#>\s+<#s', '> <', $out ) );
  192. }
  193. // include a template part
  194. public static function maybe_include_template( $template_name, $args=array(), $echo__output=false ) {
  195. // get the template from the template filename
  196. $template = apply_filters( 'qsot-locate-template', false, array( $template_name ), false, false );
  197. // extract the vars to use in this template
  198. extract( $args );
  199. // if there is no defined template, then bail
  200. if ( empty( $template ) )
  201. return '';
  202. return self::include_template( $template, $args, $echo__output, false );
  203. }
  204. // include a WC template part
  205. public static function maybe_include_wc_template( $template_name, $args=array(), $echo__output=false, $template_type ) {
  206. // get the template from the template filename
  207. $template = apply_filters( 'qsot-woo-template', false, array( $template_name ), false, false );
  208. // extract the vars to use in this template
  209. extract( $args );
  210. // if there is no defined template, then bail
  211. if ( empty( $template ) )
  212. return '';
  213. return self::include_template( $template, $args, $echo__output, false );
  214. }
  215. }
  216. if (defined('ABSPATH') && function_exists('add_action')) {
  217. QSOT_Templates::pre_init();
  218. }
  219. endif;