/wp-content/plugins/dw-question-answer/inc/Metaboxes.php

https://github.com/livinglab/openlab · PHP · 94 lines · 74 code · 7 blank · 13 comment · 10 complexity · d5bd21622a26ba1ee449a5b913565a08 MD5 · raw file

  1. <?php
  2. /**
  3. * Generate html for metabox of question status meta data
  4. * @param object $post Post Object
  5. * @return void
  6. */
  7. function dwqa_question_status_box_html( $post ){
  8. $meta = get_post_meta( $post->ID, '_dwqa_status', true );
  9. $meta = $meta ? $meta : 'open';
  10. ?>
  11. <p>
  12. <label for="dwqa-question-status">
  13. <?php _e( 'Status','dw-question-answer' ) ?><br>&nbsp;
  14. <select name="dwqa-question-status" id="dwqa-question-status" class="widefat">
  15. <option <?php selected( $meta, 'open' ); ?> value="open"><?php _e( 'Open','dw-question-answer' ) ?></option>
  16. <option <?php selected( $meta, 'pending' ); ?> value="pending"><?php _e( 'Pending','dw-question-answer' ) ?></option>
  17. <option <?php selected( $meta, 'resolved' ); ?> value="resolved"><?php _e( 'Resolved','dw-question-answer' ) ?></option>
  18. <option <?php selected( $meta, 're-open' ); ?> value="re-open"><?php _e( 'Re-Open','dw-question-answer' ) ?></option>
  19. <option <?php selected( $meta, 'closed' ); ?> value="closed"><?php _e( 'Closed','dw-question-answer' ) ?></option>
  20. </select>
  21. </label>
  22. </p>
  23. <p>
  24. <label for="dwqa-question-sticky">
  25. <?php _e( 'Sticky','dw-question-answer' ); ?><br><br>&nbsp;
  26. <?php
  27. $sticky_questions = get_option( 'dwqa_sticky_questions', array() );
  28. ?>
  29. <input <?php checked( true, in_array( $post->ID, $sticky_questions ), true ); ?> type="checkbox" name="dwqa-question-sticky" id="dwqa-question-sticky" value="1" ><span class="description"><?php _e( 'Pin question to top of archive page.','dw-question-answer' ); ?></span>
  30. </label>
  31. </p>
  32. <?php
  33. }
  34. class DWQA_Metaboxes {
  35. public function __construct() {
  36. add_action( 'add_meta_boxes', array( $this, 'answers_metabox' ) );
  37. add_filter( 'postbox_classes_dwqa-question_dwqa-answers', array( $this, 'add_css_class_metabox' ) );
  38. add_action( 'admin_init', array( $this, 'add_status_metabox' ) );
  39. add_action( 'save_post', array( $this, 'question_status_save' ) );
  40. }
  41. //Add a metabox that was used for display list of answers of a questions
  42. public function answers_metabox(){
  43. add_meta_box( 'dwqa-answers', __( 'Answers','dw-question-answer' ), array( $this, 'metabox_answers_list' ), 'dwqa-question' );
  44. }
  45. /**
  46. * generate html for metabox that was used for display list of answers of a questions
  47. */
  48. public function metabox_answers_list(){
  49. $answer_list_table = new DWQA_Answer_List_Table();
  50. $answer_list_table->display();
  51. }
  52. public function add_css_class_metabox( $classes ){
  53. $classes[] = 'dwqa-answer-list';
  54. return $classes;
  55. }
  56. /**
  57. * Add metabox for question status meta data
  58. * @return void
  59. */
  60. public function add_status_metabox(){
  61. add_meta_box( 'dwqa-post-status', __( 'Question Meta Data','dw-question-answer' ), 'dwqa_question_status_box_html', 'dwqa-question', 'side', 'high' );
  62. }
  63. public function question_status_save( $post_id ){
  64. if ( ! wp_is_post_revision( $post_id ) ) {
  65. if ( isset( $_POST['dwqa-question-status'] ) ) {
  66. update_post_meta( $post_id, '_dwqa_status', esc_html( $_POST['dwqa-question-status'] ) );
  67. }
  68. if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) {
  69. $sticky_questions = get_option( 'dwqa_sticky_questions', array() );
  70. if ( isset( $_POST['dwqa-question-sticky'] ) && sanitize_text_field( $_POST['dwqa-question-sticky'] ) ) {
  71. if ( ! in_array( $post_id, $sticky_questions ) ) {
  72. $sticky_questions[] = $post_id;
  73. update_option( 'dwqa_sticky_questions', $sticky_questions );
  74. }
  75. } else {
  76. if ( in_array( $post_id, $sticky_questions ) ) {
  77. if ( ($key = array_search( $post_id, $sticky_questions ) ) !== false ) {
  78. unset( $sticky_questions[$key] );
  79. }
  80. update_option( 'dwqa_sticky_questions', $sticky_questions );
  81. }
  82. }
  83. }
  84. }
  85. }
  86. }
  87. ?>