Pods_Templates_Auto_Template_Settings

Class Pods_Templates_Auto_Template_Settings.

Defined (1)

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

/components/Templates/includes/auto-template/Pods_Templates_Auto_Template_Settings.php  
  1. class Pods_Templates_Auto_Template_Settings { 
  2.  
  3. /** 
  4. * Front end class object 
  5. * @since 2.5.5 
  6. * @var Pods_Templates_Auto_Template_Front_End 
  7. */ 
  8. private $front_end_class; 
  9.  
  10. /** 
  11. * Holds instance of this class 
  12. * @var Pods_Templates_Auto_Template_Settings 
  13. */ 
  14. private $instance; 
  15.  
  16. /** 
  17. * Constructor for the Pods_PFAT class 
  18. * Sets up all the appropriate hooks and actions 
  19. * within the plugin. 
  20. * @since 2.5.5 
  21. */ 
  22. public function __construct() { 
  23.  
  24.  
  25. //Add option tab for post types 
  26. add_filter( 'pods_admin_setup_edit_tabs_post_type', array( $this, 'tab' ), 11, 3 ); 
  27.  
  28. //add the same tab for taxonomies 
  29. add_filter( 'pods_admin_setup_edit_tabs_taxonomy', array( $this, 'tab' ), 11, 3 ); 
  30.  
  31. //Add options to the new tab 
  32. add_filter( 'pods_admin_setup_edit_options', array( $this, 'options' ), 12, 2 ); 
  33.  
  34.  
  35. //Include and init front-end class 
  36. add_action( 'init', array( $this, 'front_end' ), 25 ); 
  37.  
  38. //Delete transients when Pods settings are updated. 
  39. add_action( 'update_option', array( $this, 'reset' ), 21, 3 ); 
  40.  
  41. //admin notice for archives without archives 
  42. add_action( 'admin_notices', array( $this, 'archive_warning' ) ); 
  43.  
  44.  
  45. /** 
  46. * Initializes the class 
  47. * @since 2.5.5 
  48. */ 
  49. public function init() { 
  50. if ( ! is_null( $this->instance ) ) { 
  51. $this->instance = new self; 
  52.  
  53. return $this->instance; 
  54.  
  55.  
  56.  
  57.  
  58. /** 
  59. * The Frontier Auto Display option tab. 
  60. * @param array $tabs 
  61. * @param array $pod 
  62. * @param array $addtl_args 
  63. * @return array 
  64. * @since 2.5.5 
  65. */ 
  66. function tab( $tabs, $pod, $addtl_args ) { 
  67.  
  68. $tabs[ 'pods-pfat' ] = __( 'Auto Template Options', 'pods' ); 
  69.  
  70. return $tabs; 
  71.  
  72.  
  73. /** 
  74. * Adds options for this plugin under the Frontier Auto Template tab. 
  75. * @param array $options 
  76. * @param array $pod 
  77. * @return array 
  78. * @since 2.5.5 
  79. */ 
  80. function options( $options, $pod ) { 
  81. //check if it's a post type pod and add fields for that. 
  82. if ( $pod['type'] === 'post_type' ) { 
  83. $options[ 'pods-pfat' ] = array ( 
  84. 'pfat_enable' => array ( 
  85. 'label' => __( 'Enable Automatic Pods Templates for this Pod?', 'pods' ),  
  86. 'help' => __( 'When enabled you can specify the names of Pods Templates to be used to display items in this Pod in the front-end.', 'pods' ),  
  87. 'type' => 'boolean',  
  88. 'default' => false,  
  89. 'dependency' => true,  
  90. 'boolean_yes_label' => '' 
  91. ),  
  92. 'pfat_single' => array ( 
  93. 'label' => __( 'Single item view template', 'pods' ),  
  94. 'help' => __( 'Name of Pods template to use for single item view.', 'pods' ),  
  95. 'type' => 'text',  
  96. 'default' => false,  
  97. 'depends-on' => array ( 'pfat_enable' => true ) 
  98. ),  
  99. 'pfat_append_single' => array ( 
  100. 'label' => __( 'Single Template Location', 'pods' ),  
  101. 'help' => __( 'Whether the template will go before, after or in place of the post content.', 'pods' ),  
  102. 'depends-on' => array ( 'pfat_enable' => true ),  
  103. ),  
  104. 'pfat_archive' => array ( 
  105. 'label' => __( 'Archive view template', 'pods' ),  
  106. 'help' => __( 'Name of Pods template to use for use in this Pods archive pages.', 'pods' ),  
  107. 'type' => 'text',  
  108. 'default' => false,  
  109. 'depends-on' => array ( 'pfat_enable' => true ) 
  110. ),  
  111. 'pfat_append_archive' => array ( 
  112. 'label' => __( 'Archive Template Location', 'pods' ),  
  113. 'help' => __( 'Whether the template will go before, after or in place of the post content.', 'pods' ),  
  114. 'depends-on' => array ( 'pfat_enable' => true ),  
  115. ),  
  116. ); 
  117.  
  118. //check if it's a taxonomy Pod, if so add fields for that 
  119. if ( $pod['type'] === 'taxonomy' ) { 
  120. $options[ 'pods-pfat' ] = array ( 
  121. 'pfat_enable' => array ( 
  122. 'label' => __( 'Enable Automatic Pods Templates for this Pod?', 'pods' ),  
  123. 'help' => __( 'When enabled you can specify the names of a Pods Template to be used to display items in this Pod in the front-end.', 'pods' ),  
  124. 'type' => 'boolean',  
  125. 'default' => false,  
  126. 'dependency' => true,  
  127. 'boolean_yes_label' => '' 
  128. ),  
  129. 'pfat_archive' => array ( 
  130. 'label' => __( 'Taxonomy Template', 'pods' ),  
  131. 'help' => __( 'Name of Pods template to use for this taxonomy.', 'pods' ),  
  132. 'type' => 'text',  
  133. 'default' => false,  
  134. 'depends-on' => array ( 'pfat_enable' => true ) 
  135. ),  
  136. 'pfat_append_archive' => array ( 
  137. 'label' => __( 'Template Location', 'pods' ),  
  138. 'help' => __( 'Whether the template will go before, after or in place of the post content.', 'pods' ),  
  139. 'depends-on' => array ( 'pfat_enable' => true ),  
  140. ),  
  141. ); 
  142.  
  143. if ( isset( $options[ 'pods-pfat' ] ) ) { 
  144.  
  145. //field options pick values 
  146. $pick = array ( 
  147. 'type' => 'pick',  
  148. 'pick_format_type' => 'single',  
  149. 'pick_format_single' => 'dropdown',  
  150. 'default' => 'true',  
  151. ); 
  152.  
  153. //get template titles 
  154. $titles = $this->get_template_titles(); 
  155.  
  156. if ( !empty( $titles ) ) { 
  157. foreach ( $pick as $k => $v ) { 
  158. $options[ 'pods-pfat' ][ 'pfat_single' ][ $k ] = $v; 
  159.  
  160. $options[ 'pods-pfat' ][ 'pfat_archive' ][ $k ] = $v; 
  161.  
  162.  
  163. $options[ 'pods-pfat' ][ 'pfat_archive' ][ 'data' ] = array( null => __('No Archive view template', 'pods') ) + ( array_combine( $this->get_template_titles(), $this->get_template_titles() ) ); 
  164. $options[ 'pods-pfat' ][ 'pfat_single' ][ 'data' ] = array_combine( $this->get_template_titles(), $this->get_template_titles() ); 
  165.  
  166. //Add data to $pick for template location 
  167. unset( $pick['data']); 
  168. $location_data = array ( 
  169. 'append' => __( 'After', 'pods' ),  
  170. 'prepend' => __( 'Before', 'pods' ),  
  171. 'replace' => __( 'Replace', 'pods' ),  
  172. ); 
  173. $pick['data'] = $location_data; 
  174.  
  175. //add location options to fields without type set. 
  176. foreach ( $options[ 'pods-pfat' ] as $k => $option ) { 
  177. if ( !isset( $option[ 'type' ] ) ) { 
  178. $options[ 'pods-pfat' ][ $k ] = array_merge( $option, $pick ); 
  179.  
  180.  
  181.  
  182. return $options; 
  183.  
  184.  
  185. /** 
  186. * Include/ init the front end class on the front end only 
  187. * @param bool $load_in_admin Optional. Whether to load in admin. Default is false. 
  188. * @return Pods_PFAT_Frontend 
  189. * @since 2.5.5 
  190. */ 
  191. function front_end( $load_in_admin = false ) { 
  192.  
  193. if ( !is_admin() || $load_in_admin ) { 
  194. include_once( dirname( __FILE__ ) . '/Pods_Templates_Auto_Template_Front_End.php' ); 
  195.  
  196. // Only instantiate if we haven't already 
  197. if ( is_null( $this->front_end_class ) ) { 
  198. $this->front_end_class = new Pods_Templates_Auto_Template_Front_End(); 
  199.  
  200. return $this->front_end_class; 
  201.  
  202.  
  203. /** 
  204. * Reset the transients for front-end class when Pods are saved. 
  205. * @uses update_option hook 
  206. * @param string $option 
  207. * @param mixed $old_value 
  208. * @param mixed $value 
  209. * @since 2.5.5 
  210. */ 
  211. function reset( $option, $old_value, $value ) { 
  212.  
  213. if ( $option === '_transient_pods_flush_rewrites' ) { 
  214. $this->reseter(); 
  215.  
  216.  
  217.  
  218. /** 
  219. * Delete transients that stores the settings. 
  220. * @since 2.5.5 
  221. */ 
  222. function reseter() { 
  223.  
  224. $keys = array( 'pods_pfat_the_pods', 'pods_pfat_auto_pods', 'pods_pfat_archive_test' ); 
  225. foreach( $keys as $key ) { 
  226. pods_transient_clear( $key ); 
  227.  
  228.  
  229. /** 
  230. * Test if archive is set for post types that don't have archives. 
  231. * @return bool|mixed|null|void 
  232. * @since 2.4.5 
  233. */ 
  234. function archive_test() { 
  235.  
  236. //try to get cached results of this method 
  237. $key = 'pods_pfat_archive_test'; 
  238. $archive_test = pods_transient_get( $key ); 
  239.  
  240. if ( $archive_test === false ) { 
  241. $front = $this->front_end( true ); 
  242. $auto_pods = $front->auto_pods(); 
  243.  
  244. foreach ( $auto_pods as $name => $pod ) { 
  245. if ( ! $pod[ 'has_archive' ] && $pod[ 'archive' ] && $pod[ 'type' ] !== 'taxonomy' && ! in_array( $name, array( 'post', 'page', 'attachment' ) ) ) { 
  246. $archive_test[ $pod[ 'label' ] ] = 'fail'; 
  247.  
  248.  
  249. pods_transient_set( $key, $archive_test ); 
  250.  
  251.  
  252. return $archive_test; 
  253.  
  254.  
  255. /** 
  256. * Throw admin warnings for post types that have archive templates set, but don't support archives 
  257. * @since 2.4.5 
  258. */ 
  259. function archive_warning() { 
  260.  
  261. //create $page variable to check if we are on pods admin page 
  262. $page = pods_v( 'page', 'get', false, true ); 
  263.  
  264. //check if we are on Pods Admin page 
  265. if ( $page === 'pods' ) { 
  266. $archive_test = $this->archive_test(); 
  267. if ( is_array( $archive_test ) ) { 
  268. foreach ( $archive_test as $label => $test ) { 
  269. if ( $test === 'fail' ) { 
  270. echo sprintf( '<div id="message" class="error"><p>%s</p></div>',  
  271. sprintf( 
  272. __( 'The Pods post type %1$s has an archive template set to be displayed using Pods auto template, but the Pod does not have an archive. You can enable post type archives in the "Advanced Options" tab.', 'pfat' ),  
  273. $label ) 
  274. ); 
  275.  
  276.  
  277.  
  278.  
  279.  
  280. /** 
  281. * Get titles of all Pods Templates 
  282. * @return string[] Array of template names 
  283. * @since 2.4.5 
  284. */ 
  285. public function get_template_titles() { 
  286.  
  287. static $template_titles; 
  288.  
  289. if ( empty( $template_titles ) ) { 
  290. $all_templates = (array) pods_api()->load_templates( array() ); 
  291.  
  292. $template_titles = array(); 
  293.  
  294. foreach ( $all_templates as $template ) { 
  295. $template_titles[] = $template['name']; 
  296.  
  297. return $template_titles; 
  298.  
  299.  
  300.