metaboxes-generator.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. /**
  3. * A wrapper for config_generator that is used to generate general-purpose metaboxes
  4. *
  5. * @package vamtam/consulting
  6. */
  7. /**
  8. * class VamtamMetaboxesGenerator
  9. */
  10. class VamtamMetaboxesGenerator extends VamtamConfigGenerator {
  11. /**
  12. * Metabox display config
  13. * @var array
  14. */
  15. private $config;
  16. /**
  17. * Metaboxes options list
  18. * @var array
  19. */
  20. protected $options;
  21. /**
  22. * Hook into the releavant actions
  23. *
  24. * @param array $config metabox display config
  25. * @param array $options metabox options list
  26. */
  27. public function __construct( $config, $options ) {
  28. $this->config = $config;
  29. $this->options = $options;
  30. if ( ! isset( $this->options['ondemand'] ) || $this->options['ondemand'] == false ) {
  31. switch ( current_filter() ) {
  32. case 'add_meta_boxes':
  33. $this->create();
  34. break;
  35. case 'save_post':
  36. $this->save( $this->config['post_id'] );
  37. break;
  38. }
  39. }
  40. }
  41. /**
  42. * Registers the metabox
  43. */
  44. protected function create() {
  45. if ( function_exists( 'add_meta_box' ) ) {
  46. if ( ! empty( $this->config['callback'] ) && function_exists( $this->config['callback'] ) )
  47. $callback = $this->config['callback'];
  48. else $callback = array( &$this, 'render' );
  49. foreach ( $this->config['pages'] as $page ) {
  50. add_meta_box( $this->config['id'], $this->config['title'], $callback, $page, $this->config['context'], $this->config['priority'] );
  51. }
  52. }
  53. }
  54. /**
  55. * Saves the post metadata
  56. * @param int $post_id current post id
  57. * @return int current post id
  58. */
  59. protected function save( $post_id ) {
  60. if ( ! isset( $_POST[ $this->config['id'] . '_noncename' ] ) )
  61. return $post_id;
  62. if ( ! wp_verify_nonce( $_POST[ $this->config['id'] . '_noncename' ], plugin_basename( __FILE__ ) ) )
  63. return $post_id;
  64. if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] && ! current_user_can( 'edit_page', $post_id ) )
  65. return $post_id;
  66. elseif ( ! current_user_can( 'edit_post', $post_id ) )
  67. return $post_id;
  68. if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
  69. return $post_id;
  70. foreach ( $this->options as $option ) {
  71. if ( isset( $option['id'] ) && ! empty( $option['id'] ) ) {
  72. if ( ! isset( $option['only'] ) ||
  73. $option['only'] == $_POST['post_type'] ||
  74. in_array( $_POST['post_type'], explode( ',', $option['only'] ) ) ) {
  75. if ( $option['type'] == 'background' || $option['type'] == 'font' ) {
  76. $suboptions = array(
  77. 'font' => array(
  78. 'size',
  79. 'lheight',
  80. 'face',
  81. 'weight',
  82. 'color',
  83. ),
  84. 'background' => array(
  85. 'image',
  86. 'color',
  87. 'position',
  88. 'attachment',
  89. 'repeat',
  90. 'size',
  91. ),
  92. );
  93. foreach ( $suboptions[ $option['type'] ] as $opt ) {
  94. $name = $option['id'] . '-' . $opt;
  95. if ( isset( $_POST[ $name ] ) ) {
  96. update_post_meta( $post_id, $name, $_POST[ $name ] );
  97. } else {
  98. delete_post_meta( $post_id, $name, get_post_meta( $post_id, $name, true ) );
  99. }
  100. }
  101. } else {
  102. if ( isset( $_POST[ $option['id'] ] ) ) {
  103. if ( $option['type'] == 'multiselect' )
  104. $value = serialize( array_unique( $_POST[ $option['id'] ] ) );
  105. else $value = $_POST[ $option['id'] ];
  106. } elseif ( $option['type'] == 'toggle' ) {
  107. $value = 'false';
  108. } else {
  109. $value = false;
  110. }
  111. if ( $value != '' ) {
  112. update_post_meta( $post_id, $option['id'], $value );
  113. } else {
  114. delete_post_meta( $post_id, $option['id'], get_post_meta( $post_id, $option['id'], true ) );
  115. }
  116. }
  117. }
  118. } elseif ( $option['type'] == 'select-row' ) {
  119. foreach ( $option['selects'] as $id => $name ) {
  120. update_post_meta( $post_id, $id, $_POST[ $id ] );
  121. }
  122. }
  123. }
  124. }
  125. /**
  126. * Renders the metabox
  127. */
  128. public function render() {
  129. global $post, $vamtam_in_metabox;
  130. $vamtam_in_metabox = true;
  131. echo '<div class="vamtam-config-group metabox">';
  132. wp_nonce_field( plugin_basename( __FILE__ ), $this->config['id'] . '_noncename' );
  133. foreach ( $this->options as $option ) {
  134. if ( ! isset( $option['only'] ) ||
  135. $option['only'] == $post->post_type ||
  136. in_array( $post->post_type, explode( ',', $option['only'] ) ) ) {
  137. if ( isset( $option['id'] ) ) {
  138. $default = get_post_meta( $post->ID, $option['id'], true );
  139. if ( $default != '' ) {
  140. $option['default'] = $default;
  141. }
  142. }
  143. if ( method_exists( $this, $option['type'] ) ) {
  144. $this->{$option['type']}( $option );
  145. } else {
  146. $this->tpl( $option['type'], $option );
  147. }
  148. }
  149. }
  150. $vamtam_in_metabox = false;
  151. echo '</div>';
  152. }
  153. }