Pods_Templates_Auto_Template_Front_End

Class Pods_Templates_Auto_Template_Front_End.

Defined (1)

The class is defined in the following location(s).

/components/Templates/includes/auto-template/Pods_Templates_Auto_Template_Front_End.php  
  1. class Pods_Templates_Auto_Template_Front_End { 
  2. function __construct() { 
  3. /** 
  4. * Allows plugin to append/replace the_excerpt 
  5. * Default is false, set to true to enable. 
  6. */ 
  7. if ( !defined( 'PFAT_USE_ON_EXCERPT' ) ) { 
  8. define( 'PFAT_USE_ON_EXCERPT', false ); 
  9.  
  10. if( !is_admin() ) { 
  11. add_action( 'wp', array( $this, 'set_frontier_style_script' ) ); 
  12.  
  13. add_filter( 'the_content', array( $this, 'front' ), 10.5 ); 
  14.  
  15. if ( PFAT_USE_ON_EXCERPT ) { 
  16. add_filter( 'the_excerpt', array ( $this, 'front' ) ); 
  17.  
  18.  
  19. /** 
  20. * Get all post type and taxonomy Pods 
  21. * @since 2.4.5 
  22. * @return array Of Pod names. 
  23. */ 
  24. function the_pods() { 
  25.  
  26. //use the cached results 
  27. $key = 'pods_pfat_the_pods'; 
  28. $the_pods = pods_transient_get( $key ); 
  29.  
  30. //check if we already have the results cached & use it if we can. 
  31. if ( false === $the_pods ) { 
  32. //get all post type pods 
  33. $the_pods = pods_api()->load_pods( array( 
  34. 'type' => array( 
  35. 'taxonomy',  
  36. 'post_type' 
  37. ),  
  38. 'names' => true ) 
  39. ); 
  40.  
  41. //cache the results 
  42. pods_transient_set( $key, $the_pods ); 
  43.  
  44.  
  45. return $the_pods; 
  46.  
  47.  
  48. /** 
  49. * Get all Pods with auto template enable and its settings 
  50. * @return array With info about auto template settings per post type 
  51. * @since 2.4.5 
  52. */ 
  53. function auto_pods() { 
  54. /** 
  55. * Filter to override all settings for which templates are used. 
  56. * Note: If this filter does not return null, all back-end settings are ignored. To add to settings with a filter, use 'pods_pfat_auto_pods'; 
  57. * @param array $auto_pods Array of parameters to use instead of those from settings. 
  58. * @return array Settings arrays for each post type. 
  59. * @since 2.4.5 
  60. */ 
  61. $auto_pods = apply_filters( 'pods_pfat_auto_pods_override', null ); 
  62. if ( !is_null( $auto_pods ) ) { 
  63. return $auto_pods; 
  64.  
  65. //try to get cached results of this method 
  66. $key = 'pods_pfat_auto_pods'; 
  67. $auto_pods = pods_transient_get( $key ); 
  68.  
  69. //check if we already have the results cached & use it if we can. 
  70. if ( $auto_pods === false ) { 
  71. //get possible pods 
  72. $the_pods = $this->the_pods(); 
  73.  
  74. //start output array empty 
  75. $auto_pods = array(); 
  76.  
  77. //get pods api class 
  78. $api = pods_api(); 
  79.  
  80. //loop through each to see if auto templates is enabled 
  81. foreach ( $the_pods as $the_pod => $the_pod_label ) { 
  82. //get this Pods' data. 
  83. $pod_data = $api->load_pod( array( 'name' => $the_pod ) ); 
  84.  
  85. //if auto template is enabled add info about Pod to array 
  86. if ( 1 == pods_v( 'pfat_enable', $pod_data[ 'options' ] ) ) { 
  87. //check if pfat_single and pfat_archive are set 
  88. $single = pods_v( 'pfat_single', $pod_data[ 'options' ], false, true ); 
  89. $archive = pods_v( 'pfat_archive', $pod_data[ 'options' ], false, true ); 
  90. $single_append = pods_v( 'pfat_append_single', $pod_data[ 'options' ], true, true ); 
  91. $archive_append = pods_v( 'pfat_append_archive', $pod_data[ 'options' ], true, true ); 
  92. $type = pods_v( 'object_type', $pod_data, false, true ); 
  93. //check if it's a post type that has an arhive 
  94. if ( $pod_data['type'] === 'post_type' && $the_pod !== 'post' || $the_pod !== 'page' ) { 
  95. $has_archive = pods_v( 'has_archive', $pod_data['options'], false, true ); 
  96. else { 
  97. $has_archive = true; 
  98.  
  99. //build output array 
  100. $auto_pods[ $the_pod ] = array( 
  101. 'name' => $the_pod,  
  102. 'label' => $the_pod_label,  
  103. 'single' => $single,  
  104. 'archive' => $archive,  
  105. 'single_append' => $single_append,  
  106. 'archive_append' => $archive_append,  
  107. 'has_archive' => $has_archive,  
  108. 'type' => $type,  
  109. ); 
  110.  
  111. } //endforeach 
  112.  
  113. //cache the results 
  114. pods_transient_set( $key, $auto_pods ); 
  115.  
  116. /** 
  117. * Add to or change settings. 
  118. * Use this filter to change or add to the settings set in the back-end for this plugin. Has no effect if 'pods_pfat_auto_pods_override' filter is being used. 
  119. * @param array $auto_pods Array of parameters to use instead of those from settings. 
  120. * @return array Settings arrays for each post type. 
  121. * @since 2.4.5 
  122. */ 
  123. $auto_pods = apply_filters( 'pods_pfat_auto_pods', $auto_pods ); 
  124.  
  125. return $auto_pods; 
  126.  
  127.  
  128. /** 
  129. * Fetches the current post type. 
  130. * @return string current post type. 
  131. * @since 2.4.5 
  132. */ 
  133. function current_post_type() { 
  134. //start by getting current post or stdClass object 
  135. global $wp_query; 
  136. $obj = $wp_query->get_queried_object(); 
  137.  
  138. //see if we are on a post type and if so, set $current_post_type to post type 
  139. if ( isset( $obj->post_type ) ) { 
  140. $current_post_type = $obj->post_type; 
  141.  
  142. elseif ( isset( $obj->taxonomy ) ) { 
  143. $current_post_type = $obj->taxonomy; 
  144. elseif ( isset ( $obj->name ) ) { 
  145. $current_post_type = $obj->name; 
  146. elseif ( is_home() ) { 
  147. $current_post_type = 'post'; 
  148. else { 
  149. $current_post_type = false; 
  150.  
  151. return $current_post_type; 
  152.  
  153. /** 
  154. * Outputs templates after the content as needed. 
  155. * @param string $content Post content 
  156. * @uses 'the_content' filter 
  157. * @return string Post content with the template appended if appropriate. 
  158. * @since 2.4.5 
  159. */ 
  160. function front( $content ) { 
  161.  
  162. // cet the current post type 
  163. $current_post_type = $this->current_post_type(); 
  164.  
  165. //now use other methods in class to build array to search in/ use 
  166. $possible_pods = $this->auto_pods(); 
  167.  
  168.  
  169. //check if $current_post_type is the key of the array of possible pods 
  170. if ( isset( $possible_pods[ $current_post_type ] ) ) { 
  171.  
  172. //build Pods object for current item 
  173. global $post; 
  174. $pods = pods( $current_post_type, $post->ID ); 
  175.  
  176. //get array for the current post type 
  177. $this_pod = $possible_pods[ $current_post_type ]; 
  178.  
  179.  
  180. if ( $this_pod[ 'single' ] && is_singular( $current_post_type ) ) { 
  181. //load the template 
  182. $content = $this->load_template( $this_pod[ 'single' ], $content , $pods, $this_pod[ 'single_append' ] ); 
  183. //if pfat_archive was set try to use that template 
  184. //check if we are on an archive of the post type 
  185. elseif ( $this_pod[ 'archive' ] && is_post_type_archive( $current_post_type ) ) { 
  186. //load the template 
  187. $content = $this->load_template( $this_pod[ 'archive' ], $content , $pods, $this_pod[ 'archive_append' ] ); 
  188.  
  189. //if pfat_archive was set and we're in the blog index, try to append template 
  190. elseif ( is_home() && $this_pod[ 'archive' ] && $current_post_type === 'post' ) { 
  191. //append the template 
  192. $content = $this->load_template( $this_pod[ 'archive' ], $content , $pods, $this_pod[ 'archive_append' ] ); 
  193.  
  194. //if is taxonomy archive of the selected taxonomy 
  195. elseif ( is_tax( $current_post_type ) ) { 
  196. //if pfat_single was set try to use that template 
  197. if ( $this_pod[ 'archive' ] ) { 
  198. //append the template 
  199. $content = $this->load_template( $this_pod[ 'archive' ], $content , $pods, $this_pod[ 'archive_append' ] ); 
  200.  
  201.  
  202.  
  203. return $content; 
  204.  
  205.  
  206. /** 
  207. * Attach Pods Template to $content 
  208. * @param string $template_name The name of a Pods Template to load. 
  209. * @param string $content Post content 
  210. * @param Pods $pods Current Pods object. 
  211. * @param bool|string $append Optional. Whether to append, prepend or replace content. Defaults to true, which appends, if false, content is replaced, if 'prepend' content is prepended. 
  212. * @return string $content with Pods Template appended if template exists 
  213. * @since 2.4.5 
  214. */ 
  215. function load_template( $template_name, $content, $pods, $append = true ) { 
  216.  
  217. //prevent infinite loops caused by this method acting on post_content 
  218. remove_filter( 'the_content', array( $this, 'front' ) ); 
  219.  
  220. /** 
  221. * Change which template -- by name -- to be used. 
  222. * @since 2.5.6 
  223. * @param string $template_name The name of a Pods Template to load. 
  224. * @param Pods $pods Current Pods object. 
  225. * @param bool|string $append Whether Template will be appended (true), prepended ("prepend") or replaced (false). 
  226. */ 
  227. $template_name = apply_filters( 'pods_auto_template_template_name', $template_name, $pods, $append ); 
  228.  
  229. $template = $pods->template( $template_name ); 
  230. add_filter( 'the_content', array( $this, 'front' ) ); 
  231.  
  232. //check if we have a valid template 
  233. if ( !is_null( $template ) ) { 
  234. //if so append it to content or replace content. 
  235.  
  236. if ( $append === 'replace' ) { 
  237. $content = $template; 
  238. elseif ( $append === 'prepend' ) { 
  239. $content = $template . $content; 
  240. elseif ( $append || $append === 'append' ) { 
  241. $content = $content . $template; 
  242. else { 
  243. $content = $template; 
  244.  
  245. return $content; 
  246.  
  247.  
  248. /** 
  249. * Sets Styles and Scripts from the Frontier template addons. 
  250. * @since 2.4.5 
  251. */ 
  252. function set_frontier_style_script() { 
  253.  
  254. if( ! class_exists( 'Pods_Frontier' ) ) { 
  255. return; 
  256.  
  257. // cet the current post type 
  258. $current_post_type = $this->current_post_type(); 
  259.  
  260. //now use other methods in class to build array to search in/ use 
  261. $possible_pods = $this->auto_pods(); 
  262.  
  263. if ( isset( $possible_pods[ $current_post_type ] ) ) { 
  264.  
  265. $this_pod = $possible_pods[ $current_post_type ]; 
  266.  
  267. if ( $this_pod[ 'single' ] && is_singular( $current_post_type ) ) { 
  268. //set template 
  269. $template = $this_pod[ 'single' ]; 
  270.  
  271. //if pfat_archive was set try to use that template 
  272. //check if we are on an archive of the post type 
  273. elseif ( $this_pod[ 'archive' ] && is_post_type_archive( $current_post_type ) ) { 
  274. //set template 
  275. $template = $this_pod[ 'archive' ]; 
  276.  
  277. //if pfat_archive was set and we're in the blog index, try to append template 
  278. elseif ( is_home() && $this_pod[ 'archive' ] && $current_post_type === 'post' ) { 
  279. //set template 
  280. $template = $this_pod[ 'archive' ]; 
  281.  
  282. //if is taxonomy archive of the selected taxonomy 
  283. elseif ( is_tax( $current_post_type ) ) { 
  284. //if pfat_single was set try to use that template 
  285. if ( $this_pod[ 'archive' ] ) { 
  286. //set template 
  287. $template = $this_pod[ 'archive' ]; 
  288.  
  289.  
  290. if( isset( $template ) ) { 
  291. global $frontier_styles, $frontier_scripts; 
  292.  
  293. $template_post = pods()->api->load_template( array('name' => $template ) ); 
  294.  
  295. if( !empty( $template_post['id'] ) ) { 
  296. // got a template - check for styles & scripts 
  297. $meta = get_post_meta($template_post['id'], 'view_template', true); 
  298.  
  299. $frontier = new Pods_Frontier; 
  300. if(!empty($meta['css'])) { 
  301. $frontier_styles .= $meta['css']; 
  302.  
  303. if(!empty($meta['js'])) { 
  304. $frontier_scripts .= $meta['js']; 
  305.