PageRenderTime 26ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/wp-pagenavi/scb/PostMetabox.php

https://bitbucket.org/ag_codeduck/site-erb-facis
PHP | 346 lines | 143 code | 54 blank | 149 comment | 16 complexity | 3686efed4e1d31e9390dc2f7c7acbc3d MD5 | raw file
Possible License(s): MIT
  1. <?php
  2. /**
  3. * Class that creates metaboxes on the post editing page.
  4. */
  5. class scbPostMetabox {
  6. /**
  7. * Metabox ID.
  8. * @var string
  9. */
  10. private $id;
  11. /**
  12. * Title.
  13. * @var string
  14. */
  15. private $title;
  16. /**
  17. * Post types.
  18. * @var array
  19. */
  20. private $post_types;
  21. /**
  22. * Post meta data.
  23. * @var array
  24. */
  25. private $post_data = array();
  26. /**
  27. * Action hooks.
  28. * @var array
  29. */
  30. protected $actions = array( 'admin_enqueue_scripts', 'post_updated_messages' );
  31. /**
  32. * Sets up metabox.
  33. *
  34. * @param string $id
  35. * @param string $title
  36. * @param array $args (optional)
  37. *
  38. * @return void
  39. */
  40. public function __construct( $id, $title, $args = array() ) {
  41. $this->id = $id;
  42. $this->title = $title;
  43. $args = wp_parse_args( $args, array(
  44. 'post_type' => 'post',
  45. 'context' => 'advanced',
  46. 'priority' => 'default',
  47. ) );
  48. if ( is_string( $args['post_type'] ) ) {
  49. $args['post_type'] = array( $args['post_type'] );
  50. }
  51. $this->post_types = $args['post_type'];
  52. $this->context = $args['context'];
  53. $this->priority = $args['priority'];
  54. add_action( 'load-post.php', array( $this, 'pre_register' ) );
  55. add_action( 'load-post-new.php', array( $this, 'pre_register' ) );
  56. }
  57. /**
  58. * Pre register the metabox.
  59. *
  60. * @return void
  61. */
  62. final public function pre_register() {
  63. if ( ! in_array( get_current_screen()->post_type, $this->post_types ) ) {
  64. return;
  65. }
  66. if ( ! $this->condition() ) {
  67. return;
  68. }
  69. if ( isset( $_GET['post'] ) ) {
  70. $this->post_data = $this->get_meta( intval( $_GET['post'] ) );
  71. }
  72. add_action( 'add_meta_boxes', array( $this, 'register' ) );
  73. add_action( 'save_post', array( $this, '_save_post' ), 10, 2 );
  74. foreach ( $this->actions as $action ) {
  75. if ( method_exists( $this, $action ) ) {
  76. add_action( $action, array( $this, $action ) );
  77. }
  78. }
  79. }
  80. /**
  81. * Additional checks before registering the metabox.
  82. *
  83. * @return bool
  84. */
  85. protected function condition() {
  86. return true;
  87. }
  88. /**
  89. * Registers the metabox.
  90. *
  91. * @return void
  92. */
  93. final public function register() {
  94. add_meta_box( $this->id, $this->title, array( $this, 'display' ), null, $this->context, $this->priority );
  95. }
  96. /**
  97. * Filter data before display.
  98. *
  99. * @param array $form_data
  100. * @param object $post
  101. *
  102. * @return array
  103. */
  104. public function before_display( $form_data, $post ) {
  105. return $form_data;
  106. }
  107. /**
  108. * Displays metabox content.
  109. *
  110. * @param object $post
  111. *
  112. * @return void
  113. */
  114. public function display( $post ) {
  115. $form_fields = $this->form_fields();
  116. if ( ! $form_fields ) {
  117. return;
  118. }
  119. $form_data = $this->post_data;
  120. $error_fields = array();
  121. if ( isset( $form_data['_error_data_' . $this->id ] ) ) {
  122. $data = maybe_unserialize( $form_data['_error_data_' . $this->id ] );
  123. $error_fields = $data['fields'];
  124. $form_data = $data['data'];
  125. $this->display_notices( $data['messages'], 'error' );
  126. }
  127. $form_data = $this->before_display( $form_data, $post );
  128. $this->before_form( $post );
  129. echo $this->table( $form_fields, $form_data, $error_fields );
  130. $this->after_form( $post );
  131. delete_post_meta( $post->ID, '_error_data_' . $this->id );
  132. }
  133. /**
  134. * Returns table.
  135. *
  136. * @param array $rows
  137. * @param array $formdata
  138. * @param array $errors (optional)
  139. *
  140. * @return string
  141. */
  142. public function table( $rows, $formdata, $errors = array() ) {
  143. $output = '';
  144. foreach ( $rows as $row ) {
  145. $output .= $this->table_row( $row, $formdata, $errors );
  146. }
  147. $output = scbForms::table_wrap( $output );
  148. return $output;
  149. }
  150. /**
  151. * Returns table row.
  152. *
  153. * @param array $row
  154. * @param array $formdata
  155. * @param array $errors (optional)
  156. *
  157. * @return string
  158. */
  159. public function table_row( $row, $formdata, $errors = array() ) {
  160. $input = scbForms::input( $row, $formdata );
  161. // If row has an error, highlight it
  162. $style = ( in_array( $row['name'], $errors ) ) ? 'style="background-color: #FFCCCC"' : '';
  163. return html( 'tr',
  164. html( "th $style scope='row'", $row['title'] ),
  165. html( "td $style", $input )
  166. );
  167. }
  168. /**
  169. * Displays notices.
  170. *
  171. * @param array|string $notices
  172. * @param string $class (optional)
  173. *
  174. * @return void
  175. */
  176. public function display_notices( $notices, $class = 'updated' ) {
  177. // add inline class so the notices stays in metabox
  178. $class .= ' inline';
  179. foreach ( (array) $notices as $notice ) {
  180. echo scb_admin_notice( $notice, $class );
  181. }
  182. }
  183. /**
  184. * Display some extra HTML before the form.
  185. *
  186. * @param object $post
  187. *
  188. * @return void
  189. */
  190. public function before_form( $post ) { }
  191. /**
  192. * Return an array of form fields.
  193. *
  194. * @return array
  195. */
  196. public function form_fields() {
  197. return array();
  198. }
  199. /**
  200. * Display some extra HTML after the form.
  201. *
  202. * @param object $post
  203. *
  204. * @return void
  205. */
  206. public function after_form( $post ) { }
  207. /**
  208. * Makes sure that the saving occurs only for the post being edited.
  209. *
  210. * @param int $post_id
  211. * @param object $post
  212. *
  213. * @return void
  214. */
  215. final public function _save_post( $post_id, $post ) {
  216. if ( ! isset( $_POST['action'] ) || $_POST['action'] != 'editpost' ) {
  217. return;
  218. }
  219. if ( ! isset( $_POST['post_ID'] ) || $_POST['post_ID'] != $post_id ) {
  220. return;
  221. }
  222. if ( ! in_array( $post->post_type, $this->post_types ) ) {
  223. return;
  224. }
  225. $this->save( $post->ID );
  226. }
  227. /**
  228. * Saves metabox form data.
  229. *
  230. * @param int $post_id
  231. *
  232. * @return void
  233. */
  234. protected function save( $post_id ) {
  235. $form_fields = $this->form_fields();
  236. $to_update = scbForms::validate_post_data( $form_fields );
  237. // Filter data
  238. $to_update = $this->before_save( $to_update, $post_id );
  239. // Validate dataset
  240. $is_valid = $this->validate_post_data( $to_update, $post_id );
  241. if ( $is_valid instanceof WP_Error && $is_valid->get_error_codes() ) {
  242. $error_data = array(
  243. 'fields' => $is_valid->get_error_codes(),
  244. 'messages' => $is_valid->get_error_messages(),
  245. 'data' => $to_update,
  246. );
  247. update_post_meta( $post_id, '_error_data_' . $this->id, $error_data );
  248. $location = add_query_arg( 'message', 1, get_edit_post_link( $post_id, 'url' ) );
  249. wp_redirect( esc_url_raw( apply_filters( 'redirect_post_location', $location, $post_id ) ) );
  250. exit;
  251. }
  252. foreach ( $to_update as $key => $value ) {
  253. update_post_meta( $post_id, $key, $value );
  254. }
  255. }
  256. /**
  257. * Filter data before save.
  258. *
  259. * @param array $post_data
  260. * @param int $post_id
  261. *
  262. * @return array
  263. */
  264. protected function before_save( $post_data, $post_id ) {
  265. return $post_data;
  266. }
  267. /**
  268. * Validate posted data.
  269. *
  270. * @param array $post_data
  271. * @param int $post_id
  272. *
  273. * @return bool|object A WP_Error object if posted data are invalid.
  274. */
  275. protected function validate_post_data( $post_data, $post_id ) {
  276. return false;
  277. }
  278. /**
  279. * Returns an array of post meta.
  280. *
  281. * @param int $post_id
  282. *
  283. * @return array
  284. */
  285. private function get_meta( $post_id ) {
  286. $meta = get_post_custom( $post_id );
  287. foreach ( $meta as $key => $values ) {
  288. $meta[ $key ] = maybe_unserialize( $meta[ $key ][0] );
  289. }
  290. return $meta;
  291. }
  292. }