config-generator.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /**
  3. * A meta programming helper used to generated the html code needed for a configuration page
  4. *
  5. * @package vamtam/consulting
  6. */
  7. /**
  8. * class VamtamConfigGenerator
  9. */
  10. class VamtamConfigGenerator {
  11. /**
  12. * Options page name
  13. * @var string
  14. */
  15. public $name;
  16. /**
  17. * Options page config
  18. * @var array
  19. */
  20. protected $options;
  21. /**
  22. * Initialize the generator
  23. *
  24. * @param string $name
  25. * @param array $options definitions for the config page
  26. */
  27. public function __construct( $name, $options ) {
  28. $this->name = $name;
  29. $this->options = $options;
  30. if ( isset( $_POST['save-vamtam-config'] ) )
  31. $this->save_config();
  32. $this->render();
  33. }
  34. /**
  35. * Save the current page config
  36. */
  37. private function save_config() {
  38. vamtam_save_config( $this->options );
  39. global $vamtam_config_messages;
  40. $vamtam_config_messages .= '<div class="message updated fade"><p><strong>Updated Successfully</strong></p></div>';
  41. }
  42. /**
  43. * Single options row template
  44. * @param string $template template name
  45. * @param array $value options row config
  46. */
  47. protected function tpl( $template, $value ) {
  48. extract( $value );
  49. if ( ! isset( $desc ) )
  50. $desc = '';
  51. if ( ! isset( $default ) )
  52. $default = null;
  53. if ( ! isset( $class ) )
  54. $class = '';
  55. include VAMTAM_ADMIN_HELPERS . "config_generator/$template.php";
  56. }
  57. /**
  58. * Renders the option page
  59. */
  60. private function render() {
  61. echo '<div class="wrap vamtam-config-page">';
  62. echo '<form method="post" action="">';
  63. if ( isset( $_GET['allowreset'] ) ) {
  64. echo '<input type="hidden" name="doreset" value="true" />';
  65. }
  66. if ( isset( $_GET['cacheonly'] ) ) {
  67. echo '<input type="hidden" name="cacheonly" value="true" />';
  68. }
  69. foreach ( $this->options as $option ) {
  70. if ( method_exists( $this, $option['type'] ) ) {
  71. $this->{$option['type']}( $option );
  72. }
  73. else {
  74. $this->tpl( $option['type'], $option );
  75. }
  76. }
  77. echo '</div>'; // #theme-config
  78. if ( ! isset( $this->options[0]['no-save-button'] ) ) {
  79. $this->tpl( 'save', array() );
  80. }
  81. echo '</form>';
  82. echo '</div>';
  83. }
  84. /**
  85. * Auto fill <select> options
  86. * @param string $type autofill type
  87. * @return array options list
  88. */
  89. public static function get_select_target_config( $type ) {
  90. return self::target_config( $type );
  91. }
  92. /**
  93. * Auto fill <select> options
  94. * @param string $type autofill type
  95. * @return array options list
  96. */
  97. public static function target_config( $type ) {
  98. $config = array();
  99. switch ( $type ) {
  100. case 'page':
  101. $entries = get_pages( 'title_li=&orderby=name' );
  102. foreach ( $entries as $key => $entry )
  103. $config[ $entry->ID ] = $entry->post_title;
  104. break;
  105. case 'cat':
  106. $entries = get_categories( 'orderby=name&hide_empty=0' );
  107. foreach ( $entries as $key => $entry )
  108. $config[ $entry->term_id ] = $entry->name;
  109. break;
  110. case 'post':
  111. $entries = get_posts( 'orderby=title&numberposts=-1&order=ASC' );
  112. foreach ( $entries as $key => $entry )
  113. $config[ $entry->ID ] = $entry->post_title;
  114. break;
  115. case 'portfolio':
  116. $entries = get_posts( 'post_type=jetpack-portfolio&orderby=title&numberposts=-1&order=ASC' );
  117. foreach ( $entries as $key => $entry )
  118. $config[ $entry->ID ] = $entry->post_title;
  119. break;
  120. case 'portfolio-type':
  121. $entries = get_terms( 'jetpack-portfolio-type', 'orderby=name&hide_empty=0' );
  122. foreach ( $entries as $key => $entry )
  123. $config[ $entry->slug ] = $entry->name;
  124. break;
  125. case 'testimonials':
  126. $entries = get_posts( 'post_type=jetpack-testimonial&orderby=title&numberposts=-1&order=ASC' );
  127. foreach ( $entries as $key => $entry )
  128. $config[ $entry->ID ] = $entry->post_title;
  129. break;
  130. }
  131. return $config;
  132. }
  133. }