← All changes
|
Libs/adminify-framework/classes/comment-options.class.php
+223
-273
4.2.13
→
3.2.4.1
View file →
| @@ -1,5 +1,6 @@ | ||
| 1 | -<?php if ( ! defined( 'ABSPATH' ) ) { die; } // Cannot access directly. | |
| 1 | +<?php if ( ! defined( 'ABSPATH' ) ) { | |
| 2 | + die; } // Cannot access directly. | |
| 2 | 3 | /** |
| 3 | 4 | * |
| 4 | 5 | * Comment Metabox Class |
| 5 | 6 | * |
| @@ -4,341 +5,290 @@ | ||
| 4 | 5 | * Comment Metabox Class |
| 5 | 6 | * |
| 6 | 7 | * @since 1.0.0 |
| 7 | 8 | * @version 1.0.0 |
| 8 | - * | |
| 9 | 9 | */ |
| 10 | 10 | if ( ! class_exists( 'ADMINIFY_Comment_Metabox' ) ) { |
| 11 | - class ADMINIFY_Comment_Metabox extends ADMINIFY_Abstract{ | |
| 11 | + class ADMINIFY_Comment_Metabox extends ADMINIFY_Abstract { | |
| 12 | 12 | |
| 13 | - // constans | |
| 14 | - public $unique = ''; | |
| 15 | - public $abstract = 'comment_metabox'; | |
| 16 | - public $sections = array(); | |
| 17 | - public $pre_fields = array(); | |
| 18 | - public $args = array( | |
| 19 | - 'title' => '', | |
| 20 | - 'data_type' => 'serialize', | |
| 21 | - 'priority' => 'default', | |
| 22 | - 'show_reset' => false, | |
| 23 | - 'show_restore' => false, | |
| 24 | - 'nav' => 'normal', | |
| 25 | - 'theme' => 'dark', | |
| 26 | - 'class' => '', | |
| 27 | - 'defaults' => array(), | |
| 28 | - ); | |
| 13 | + // constans | |
| 14 | + public $unique = ''; | |
| 15 | + public $abstract = 'comment_metabox'; | |
| 16 | + public $pre_fields = []; | |
| 17 | + public $sections = []; | |
| 18 | + public $args = [ | |
| 19 | + 'title' => '', | |
| 20 | + 'data_type' => 'serialize', | |
| 21 | + 'priority' => 'default', | |
| 22 | + 'show_reset' => false, | |
| 23 | + 'show_restore' => false, | |
| 24 | + 'nav' => 'normal', | |
| 25 | + 'theme' => 'dark', | |
| 26 | + 'class' => '', | |
| 27 | + 'defaults' => [], | |
| 28 | + ]; | |
| 29 | 29 | |
| 30 | - // run comment metabox construct | |
| 31 | - public function __construct( $key, $params = array() ) { | |
| 30 | + // run comment metabox construct | |
| 31 | + public function __construct( $key, $params = [] ) { | |
| 32 | + $this->unique = $key; | |
| 33 | + $this->args = apply_filters( "adminify_{$this->unique}_args", wp_parse_args( $params['args'], $this->args ), $this ); | |
| 34 | + $this->sections = apply_filters( "adminify_{$this->unique}_sections", $params['sections'], $this ); | |
| 35 | + $this->pre_fields = $this->pre_fields( $this->sections ); | |
| 32 | 36 | |
| 33 | - $this->unique = $key; | |
| 34 | - $this->args = apply_filters( "adminify_{$this->unique}_args", wp_parse_args( $params['args'], $this->args ), $this ); | |
| 35 | - $this->sections = apply_filters( "adminify_{$this->unique}_sections", $params['sections'], $this ); | |
| 36 | - $this->pre_fields = $this->pre_fields( $this->sections ); | |
| 37 | + add_action( 'add_meta_boxes_comment', [ $this, 'add_comment_meta_box' ] ); | |
| 38 | + add_action( 'edit_comment', [ $this, 'save_comment_meta_box' ] ); | |
| 37 | 39 | |
| 38 | - add_action( 'add_meta_boxes_comment', array( $this, 'add_comment_meta_box' ) ); | |
| 39 | - add_action( 'edit_comment', array( $this, 'save_comment_meta_box' ) ); | |
| 40 | + if ( ! empty( $this->args['class'] ) ) { | |
| 41 | + add_filter( 'postbox_classes_comment_' . $this->unique, [ $this, 'add_comment_metabox_classes' ] ); | |
| 42 | + } | |
| 43 | + } | |
| 40 | 44 | |
| 41 | - if ( ! empty( $this->args['class'] ) ) { | |
| 42 | - add_filter( 'postbox_classes_comment_'. $this->unique, array( $this, 'add_comment_metabox_classes' ) ); | |
| 43 | - } | |
| 45 | + // instance | |
| 46 | + public static function instance( $key, $params = [] ) { | |
| 47 | + return new self( $key, $params ); | |
| 48 | + } | |
| 44 | 49 | |
| 45 | - } | |
| 50 | + public function pre_fields( $sections ) { | |
| 51 | + $result = []; | |
| 46 | 52 | |
| 47 | - // instance | |
| 48 | - public static function instance( $key, $params = array() ) { | |
| 49 | - return new self( $key, $params ); | |
| 50 | - } | |
| 53 | + foreach ( $sections as $key => $section ) { | |
| 54 | + if ( ! empty( $section['fields'] ) ) { | |
| 55 | + foreach ( $section['fields'] as $field ) { | |
| 56 | + $result[] = $field; | |
| 57 | + } | |
| 58 | + } | |
| 59 | + } | |
| 51 | 60 | |
| 52 | - public function add_comment_metabox_classes( $classes ) { | |
| 61 | + return $result; | |
| 62 | + } | |
| 53 | 63 | |
| 54 | - if ( ! empty( $this->args['class'] ) ) { | |
| 55 | - $classes[] = $this->args['class']; | |
| 56 | - } | |
| 64 | + public function add_comment_metabox_classes( $classes ) { | |
| 65 | + if ( ! empty( $this->args['class'] ) ) { | |
| 66 | + $classes[] = $this->args['class']; | |
| 67 | + } | |
| 57 | 68 | |
| 58 | - return $classes; | |
| 69 | + return $classes; | |
| 70 | + } | |
| 59 | 71 | |
| 60 | - } | |
| 72 | + // add comment metabox | |
| 73 | + public function add_comment_meta_box( $post_type ) { | |
| 74 | + add_meta_box( $this->unique, $this->args['title'], [ $this, 'add_comment_meta_box_content' ], 'comment', 'normal', $this->args['priority'], $this->args ); | |
| 75 | + } | |
| 61 | 76 | |
| 62 | - // add comment metabox | |
| 63 | - public function add_comment_meta_box( $post_type ) { | |
| 77 | + // get default value | |
| 78 | + public function get_default( $field ) { | |
| 79 | + $default = ( isset( $field['default'] ) ) ? $field['default'] : ''; | |
| 80 | + $default = ( isset( $this->args['defaults'][ $field['id'] ] ) ) ? $this->args['defaults'][ $field['id'] ] : $default; | |
| 64 | 81 | |
| 65 | - add_meta_box( $this->unique, $this->args['title'], array( $this, 'add_comment_meta_box_content' ), 'comment', 'normal', $this->args['priority'], $this->args ); | |
| 82 | + return $default; | |
| 83 | + } | |
| 66 | 84 | |
| 67 | - } | |
| 85 | + // get meta value | |
| 86 | + public function get_meta_value( $comment_id, $field ) { | |
| 87 | + $value = null; | |
| 68 | 88 | |
| 69 | - // get default value | |
| 70 | - public function get_default( $field ) { | |
| 89 | + if ( ! empty( $comment_id ) && ! empty( $field['id'] ) ) { | |
| 90 | + if ( $this->args['data_type'] !== 'serialize' ) { | |
| 91 | + $meta = get_comment_meta( $comment_id, $field['id'] ); | |
| 92 | + $value = ( isset( $meta[0] ) ) ? $meta[0] : null; | |
| 93 | + } else { | |
| 94 | + $meta = get_comment_meta( $comment_id, $this->unique, true ); | |
| 95 | + $value = ( isset( $meta[ $field['id'] ] ) ) ? $meta[ $field['id'] ] : null; | |
| 96 | + } | |
| 97 | + } | |
| 71 | 98 | |
| 72 | - $default = ( isset( $field['default'] ) ) ? $field['default'] : ''; | |
| 73 | - $default = ( isset( $this->args['defaults'][$field['id']] ) ) ? $this->args['defaults'][$field['id']] : $default; | |
| 99 | + $default = ( isset( $field['id'] ) ) ? $this->get_default( $field ) : ''; | |
| 100 | + $value = ( isset( $value ) ) ? $value : $default; | |
| 74 | 101 | |
| 75 | - return $default; | |
| 102 | + return $value; | |
| 103 | + } | |
| 76 | 104 | |
| 77 | - } | |
| 105 | + // add comment metabox content | |
| 106 | + public function add_comment_meta_box_content( $comment, $callback ) { | |
| 107 | + $has_nav = ( count( $this->sections ) > 1 ) ? true : false; | |
| 108 | + $show_all = ( ! $has_nav ) ? ' adminify-show-all' : ''; | |
| 109 | + $errors = ( is_object( $comment ) ) ? get_comment_meta( $comment->comment_ID, '_adminify_errors_' . $this->unique, true ) : []; | |
| 110 | + $errors = ( ! empty( $errors ) ) ? $errors : []; | |
| 111 | + $theme = ( $this->args['theme'] ) ? ' adminify-theme-' . $this->args['theme'] : ''; | |
| 112 | + $nav_type = ( $this->args['nav'] === 'inline' ) ? 'inline' : 'normal'; | |
| 78 | 113 | |
| 79 | - // get meta value | |
| 80 | - public function get_meta_value( $comment_id, $field ) { | |
| 114 | + if ( is_object( $comment ) && ! empty( $errors ) ) { | |
| 115 | + delete_comment_meta( $comment->comment_ID, '_adminify_errors_' . $this->unique ); | |
| 116 | + } | |
| 81 | 117 | |
| 82 | - $value = null; | |
| 118 | + wp_nonce_field( 'adminify_comment_metabox_nonce', 'adminify_comment_metabox_nonce' . $this->unique ); | |
| 83 | 119 | |
| 84 | - if ( ! empty( $comment_id ) && ! empty( $field['id'] ) ) { | |
| 120 | + echo '<div class="adminify adminify-comment-metabox' . esc_attr( $theme ) . '">'; | |
| 85 | 121 | |
| 86 | - if ( $this->args['data_type'] !== 'serialize' ) { | |
| 87 | - $meta = get_comment_meta( $comment_id, $field['id'] ); | |
| 88 | - $value = ( isset( $meta[0] ) ) ? $meta[0] : null; | |
| 89 | - } else { | |
| 90 | - $meta = get_comment_meta( $comment_id, $this->unique, true ); | |
| 91 | - $value = ( isset( $meta[$field['id']] ) ) ? $meta[$field['id']] : null; | |
| 92 | - } | |
| 122 | + echo '<div class="adminify-wrapper' . esc_attr( $show_all ) . '">'; | |
| 93 | 123 | |
| 94 | - } | |
| 124 | + if ( $has_nav ) { | |
| 125 | + echo '<div class="adminify-nav adminify-nav-' . esc_attr( $nav_type ) . ' adminify-nav-metabox">'; | |
| 95 | 126 | |
| 96 | - $default = ( isset( $field['id'] ) ) ? $this->get_default( $field ) : ''; | |
| 97 | - $value = ( isset( $value ) ) ? $value : $default; | |
| 127 | + echo '<ul>'; | |
| 98 | 128 | |
| 99 | - return $value; | |
| 129 | + $tab_key = 1; | |
| 100 | 130 | |
| 101 | - } | |
| 131 | + foreach ( $this->sections as $section ) { | |
| 132 | + $tab_icon = ( ! empty( $section['icon'] ) ) ? '<i class="adminify-tab-icon ' . esc_attr( $section['icon'] ) . '"></i>' : ''; | |
| 133 | + $tab_error = ( ! empty( $errors['sections'][ $tab_key ] ) ) ? '<i class="adminify-label-error adminify-error">!</i>' : ''; | |
| 102 | 134 | |
| 103 | - // add comment metabox content | |
| 104 | - public function add_comment_meta_box_content( $comment, $callback ) { | |
| 135 | + echo '<li><a href="#">' . wp_kses_post( $tab_icon ) . esc_attr( $section['title'] ) . wp_kses_post( $tab_error ) . '</a></li>'; | |
| 105 | 136 | |
| 106 | - $has_nav = ( count( $this->sections ) > 1 ) ? true : false; | |
| 107 | - $show_all = ( ! $has_nav ) ? ' adminify-show-all' : ''; | |
| 108 | - $errors = ( is_object ( $comment ) ) ? get_comment_meta( $comment->comment_ID, '_adminify_errors_'. $this->unique, true ) : array(); | |
| 109 | - $errors = ( ! empty( $errors ) ) ? $errors : array(); | |
| 110 | - $theme = ( $this->args['theme'] ) ? ' adminify-theme-'. $this->args['theme'] : ''; | |
| 111 | - $nav_type = ( $this->args['nav'] === 'inline' ) ? 'inline' : 'normal'; | |
| 137 | + $tab_key++; | |
| 138 | + } | |
| 112 | 139 | |
| 113 | - if ( is_object( $comment ) && ! empty( $errors ) ) { | |
| 114 | - delete_comment_meta( $comment->comment_ID, '_adminify_errors_'. $this->unique ); | |
| 115 | - } | |
| 140 | + echo '</ul>'; | |
| 116 | 141 | |
| 117 | - wp_nonce_field( 'adminify_comment_metabox_nonce', 'adminify_comment_metabox_nonce'. $this->unique ); | |
| 142 | + echo '</div>'; | |
| 143 | + } | |
| 118 | 144 | |
| 119 | - echo '<div class="adminify adminify-comment-metabox'. esc_attr( $theme ) .'">'; | |
| 145 | + echo '<div class="adminify-content">'; | |
| 120 | 146 | |
| 121 | - echo '<div class="adminify-wrapper'. esc_attr( $show_all ) .'">'; | |
| 147 | + echo '<div class="adminify-sections">'; | |
| 122 | 148 | |
| 123 | - if ( $has_nav ) { | |
| 149 | + $section_key = 1; | |
| 124 | 150 | |
| 125 | - echo '<div class="adminify-nav adminify-nav-'. esc_attr( $nav_type ) .' adminify-nav-metabox">'; | |
| 151 | + foreach ( $this->sections as $section ) { | |
| 152 | + $section_onload = ( ! $has_nav ) ? ' adminify-onload' : ''; | |
| 153 | + $section_class = ( ! empty( $section['class'] ) ) ? ' ' . $section['class'] : ''; | |
| 154 | + $section_title = ( ! empty( $section['title'] ) ) ? $section['title'] : ''; | |
| 155 | + $section_icon = ( ! empty( $section['icon'] ) ) ? '<i class="adminify-section-icon ' . esc_attr( $section['icon'] ) . '"></i>' : ''; | |
| 126 | 156 | |
| 127 | - echo '<ul>'; | |
| 157 | + echo '<div class="adminify-section hidden' . esc_attr( $section_onload . $section_class ) . '">'; | |
| 128 | 158 | |
| 129 | - $tab_key = 1; | |
| 159 | + echo ( $section_title || $section_icon ) ? '<div class="adminify-section-title"><h3>' . wp_kses_post( $section_icon . $section_title ) . '</h3></div>' : ''; | |
| 160 | + echo ( ! empty( $section['description'] ) ) ? '<div class="adminify-field adminify-section-description">' . wp_kses_post( $section['description'] ) . '</div>' : ''; | |
| 130 | 161 | |
| 131 | - foreach ( $this->sections as $section ) { | |
| 162 | + if ( ! empty( $section['fields'] ) ) { | |
| 163 | + foreach ( $section['fields'] as $field ) { | |
| 164 | + if ( ! empty( $field['id'] ) && ! empty( $errors['fields'][ $field['id'] ] ) ) { | |
| 165 | + $field['_error'] = $errors['fields'][ $field['id'] ]; | |
| 166 | + } | |
| 132 | 167 | |
| 133 | - $tab_icon = ( ! empty( $section['icon'] ) ) ? '<i class="adminify-tab-icon '. esc_attr( $section['icon'] ) .'"></i>' : ''; | |
| 134 | - $tab_error = ( ! empty( $errors['sections'][$tab_key] ) ) ? '<i class="adminify-label-error adminify-error">!</i>' : ''; | |
| 168 | + if ( ! empty( $field['id'] ) ) { | |
| 169 | + $field['default'] = $this->get_default( $field ); | |
| 170 | + } | |
| 135 | 171 | |
| 136 | - echo '<li><a href="#">'. wp_kses_post( $tab_icon ) . esc_html( $section['title'] ) . wp_kses_post( $tab_error ) .'</a></li>'; | |
| 172 | + ADMINIFY::field( $field, $this->get_meta_value( $comment->comment_ID, $field ), $this->unique, 'comment_metabox' ); | |
| 173 | + } | |
| 174 | + } else { | |
| 175 | + echo '<div class="adminify-no-option">' . esc_html__( 'No data available.', 'adminify' ) . '</div>'; | |
| 176 | + } | |
| 137 | 177 | |
| 138 | - $tab_key++; | |
| 178 | + echo '</div>'; | |
| 139 | 179 | |
| 140 | - } | |
| 180 | + $section_key++; | |
| 181 | + } | |
| 141 | 182 | |
| 142 | - echo '</ul>'; | |
| 183 | + echo '</div>'; | |
| 143 | 184 | |
| 144 | - echo '</div>'; | |
| 185 | + if ( ! empty( $this->args['show_restore'] ) || ! empty( $this->args['show_reset'] ) ) { | |
| 186 | + echo '<div class="adminify-sections-reset">'; | |
| 187 | + echo '<label>'; | |
| 188 | + echo '<input type="checkbox" name="' . esc_attr( $this->unique ) . '[_reset]" />'; | |
| 189 | + echo '<span class="button adminify-button-reset">' . esc_html__( 'Reset', 'adminify' ) . '</span>'; | |
| 190 | + echo '<span class="button adminify-button-cancel">' . sprintf( '<small>( %s )</small> %s', esc_html__( 'update post', 'adminify' ), esc_html__( 'Cancel', 'adminify' ) ) . '</span>'; | |
| 191 | + echo '</label>'; | |
| 192 | + echo '</div>'; | |
| 193 | + } | |
| 145 | 194 | |
| 146 | - } | |
| 195 | + echo '</div>'; | |
| 147 | 196 | |
| 148 | - echo '<div class="adminify-content">'; | |
| 197 | + echo ( $has_nav && $nav_type === 'normal' ) ? '<div class="adminify-nav-background"></div>' : ''; | |
| 149 | 198 | |
| 150 | - echo '<div class="adminify-sections">'; | |
| 199 | + echo '<div class="clear"></div>'; | |
| 151 | 200 | |
| 152 | - $section_key = 1; | |
| 201 | + echo '</div>'; | |
| 153 | 202 | |
| 154 | - foreach ( $this->sections as $section ) { | |
| 203 | + echo '</div>'; | |
| 204 | + } | |
| 155 | 205 | |
| 156 | - $section_onload = ( ! $has_nav ) ? ' adminify-onload' : ''; | |
| 157 | - $section_class = ( ! empty( $section['class'] ) ) ? ' '. $section['class'] : ''; | |
| 158 | - $section_title = ( ! empty( $section['title'] ) ) ? $section['title'] : ''; | |
| 159 | - $section_icon = ( ! empty( $section['icon'] ) ) ? '<i class="adminify-section-icon '. esc_attr( $section['icon'] ) .'"></i>' : ''; | |
| 206 | + // save comment metabox | |
| 207 | + public function save_comment_meta_box( $comment_id ) { | |
| 208 | + $count = 1; | |
| 209 | + $data = []; | |
| 210 | + $errors = []; | |
| 211 | + $noncekey = 'adminify_comment_metabox_nonce' . $this->unique; | |
| 212 | + $nonce = ( ! empty( $_POST[ $noncekey ] ) ) ? sanitize_text_field( wp_unslash( $_POST[ $noncekey ] ) ) : ''; | |
| 160 | 213 | |
| 161 | - echo '<div class="adminify-section hidden'. esc_attr( $section_onload . $section_class ) .'">'; | |
| 214 | + if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || ! wp_verify_nonce( $nonce, 'adminify_comment_metabox_nonce' ) ) { | |
| 215 | + return $comment_id; | |
| 216 | + } | |
| 162 | 217 | |
| 163 | - echo ( $section_title || $section_icon ) ? '<div class="adminify-section-title"><h3>'. wp_kses_post( $section_icon ) . esc_html( $section_title ) .'</h3></div>' : ''; | |
| 164 | - echo ( ! empty( $section['description'] ) ) ? '<div class="adminify-field adminify-section-description">'. wp_kses_post( $section['description'] ) .'</div>' : ''; | |
| 218 | + if ( ! empty( $_POST[ $this->unique ] ) ) { | |
| 219 | + $request = sanitize_text_field( wp_unslash( $_POST[ $this->unique ] ) ); | |
| 220 | + } else { | |
| 221 | + $request = []; | |
| 222 | + } | |
| 165 | 223 | |
| 166 | - if ( ! empty( $section['fields'] ) ) { | |
| 224 | + if ( ! empty( $request ) ) { | |
| 225 | + foreach ( $this->sections as $section ) { | |
| 226 | + if ( ! empty( $section['fields'] ) ) { | |
| 227 | + foreach ( $section['fields'] as $field ) { | |
| 228 | + if ( ! empty( $field['id'] ) ) { | |
| 229 | + $field_id = $field['id']; | |
| 230 | + $field_value = isset( $request[ $field_id ] ) ? $request[ $field_id ] : ''; | |
| 167 | 231 | |
| 168 | - foreach ( $section['fields'] as $field ) { | |
| 232 | + // Sanitize "post" request of field. | |
| 233 | + if ( ! isset( $field['sanitize'] ) ) { | |
| 234 | + if ( is_array( $field_value ) ) { | |
| 235 | + $data[ $field_id ] = wp_kses_post_deep( $field_value ); | |
| 236 | + } else { | |
| 237 | + $data[ $field_id ] = wp_kses_post( $field_value ); | |
| 238 | + } | |
| 239 | + } elseif ( isset( $field['sanitize'] ) && is_callable( $field['sanitize'] ) ) { | |
| 240 | + $data[ $field_id ] = call_user_func( $field['sanitize'], $field_value ); | |
| 241 | + } else { | |
| 242 | + $data[ $field_id ] = $field_value; | |
| 243 | + } | |
| 169 | 244 | |
| 170 | - if ( ! empty( $field['id'] ) && ! empty( $errors['fields'][$field['id']] ) ) { | |
| 171 | - $field['_error'] = $errors['fields'][$field['id']]; | |
| 172 | - } | |
| 245 | + // Validate "post" request of field. | |
| 246 | + if ( isset( $field['validate'] ) && is_callable( $field['validate'] ) ) { | |
| 247 | + $has_validated = call_user_func( $field['validate'], $field_value ); | |
| 173 | 248 | |
| 174 | - if ( ! empty( $field['id'] ) ) { | |
| 175 | - $field['default'] = $this->get_default( $field ); | |
| 176 | - } | |
| 249 | + if ( ! empty( $has_validated ) ) { | |
| 250 | + $errors['sections'][ $count ] = true; | |
| 251 | + $errors['fields'][ $field_id ] = $has_validated; | |
| 252 | + $data[ $field_id ] = $this->get_meta_value( $comment_id, $field ); | |
| 253 | + } | |
| 254 | + } | |
| 255 | + } | |
| 256 | + } | |
| 257 | + } | |
| 177 | 258 | |
| 178 | - ADMINIFY::field( $field, $this->get_meta_value( $comment->comment_ID, $field ), $this->unique, 'comment_metabox' ); | |
| 259 | + $count++; | |
| 260 | + } | |
| 261 | + } | |
| 179 | 262 | |
| 180 | - } | |
| 263 | + $data = apply_filters( "adminify_{$this->unique}_save", $data, $comment_id, $this ); | |
| 181 | 264 | |
| 182 | - } else { | |
| 265 | + do_action( "adminify_{$this->unique}_save_before", $data, $comment_id, $this ); | |
| 183 | 266 | |
| 184 | - echo '<div class="adminify-no-option">'. esc_html__( 'No data available.', 'adminify' ) .'</div>'; | |
| 267 | + if ( empty( $data ) || ! empty( $request['_reset'] ) ) { | |
| 268 | + if ( $this->args['data_type'] !== 'serialize' ) { | |
| 269 | + foreach ( $data as $key => $value ) { | |
| 270 | + delete_comment_meta( $comment_id, $key ); | |
| 271 | + } | |
| 272 | + } else { | |
| 273 | + delete_comment_meta( $comment_id, $this->unique ); | |
| 274 | + } | |
| 275 | + } else { | |
| 276 | + if ( $this->args['data_type'] !== 'serialize' ) { | |
| 277 | + foreach ( $data as $key => $value ) { | |
| 278 | + update_comment_meta( $comment_id, $key, $value ); | |
| 279 | + } | |
| 280 | + } else { | |
| 281 | + update_comment_meta( $comment_id, $this->unique, $data ); | |
| 282 | + } | |
| 185 | 283 | |
| 186 | - } | |
| 284 | + if ( ! empty( $errors ) ) { | |
| 285 | + update_comment_meta( $comment_id, '_adminify_errors_' . $this->unique, $errors ); | |
| 286 | + } | |
| 287 | + } | |
| 187 | 288 | |
| 188 | - echo '</div>'; | |
| 289 | + do_action( "adminify_{$this->unique}_saved", $data, $comment_id, $this ); | |
| 189 | 290 | |
| 190 | - $section_key++; | |
| 191 | - | |
| 192 | - } | |
| 193 | - | |
| 194 | - echo '</div>'; | |
| 195 | - | |
| 196 | - if ( ! empty( $this->args['show_restore'] ) || ! empty( $this->args['show_reset'] ) ) { | |
| 197 | - | |
| 198 | - echo '<div class="adminify-sections-reset">'; | |
| 199 | - echo '<label>'; | |
| 200 | - echo '<input type="checkbox" name="'. esc_attr( $this->unique ) .'[_reset]" />'; | |
| 201 | - echo '<span class="button adminify-button-reset">'. esc_html__( 'Reset', 'adminify' ) .'</span>'; | |
| 202 | - echo '<span class="button adminify-button-cancel">'. sprintf( '<small>( %s )</small> %s', esc_html__( 'update post', 'adminify' ), esc_html__( 'Cancel', 'adminify' ) ) .'</span>'; | |
| 203 | - echo '</label>'; | |
| 204 | - echo '</div>'; | |
| 205 | - | |
| 206 | - } | |
| 207 | - | |
| 208 | - echo '</div>'; | |
| 209 | - | |
| 210 | - echo ( $has_nav && $nav_type === 'normal' ) ? '<div class="adminify-nav-background"></div>' : ''; | |
| 211 | - | |
| 212 | - echo '<div class="clear"></div>'; | |
| 213 | - | |
| 214 | - echo '</div>'; | |
| 215 | - | |
| 216 | - echo '</div>'; | |
| 217 | - | |
| 218 | - } | |
| 219 | - | |
| 220 | - // save comment metabox | |
| 221 | - public function save_comment_meta_box( $comment_id ) { | |
| 222 | - | |
| 223 | - $count = 1; | |
| 224 | - $data = array(); | |
| 225 | - $errors = array(); | |
| 226 | - $noncekey = 'adminify_comment_metabox_nonce'. $this->unique; | |
| 227 | - $nonce = ( ! empty( $_POST[ $noncekey ] ) ) ? sanitize_text_field( wp_unslash( $_POST[ $noncekey ] ) ) : ''; | |
| 228 | - | |
| 229 | - if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || ! wp_verify_nonce( $nonce, 'adminify_comment_metabox_nonce' ) ) { | |
| 230 | - return $comment_id; | |
| 231 | - } | |
| 232 | - | |
| 233 | - // Authorization: a valid nonce proves intent, not permission. | |
| 234 | - if ( ! current_user_can( 'edit_comment', $comment_id ) ) { | |
| 235 | - return $comment_id; | |
| 236 | - } | |
| 237 | - | |
| 238 | - // XSS ok. | |
| 239 | - // No worries, This "POST" requests is sanitizing in the below foreach. | |
| 240 | - $request = ( ! empty( $_POST[ $this->unique ] ) ) ? wp_unslash( $_POST[ $this->unique ] ) : array(); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- each field is sanitized individually by the framework's per-field sanitize handlers. | |
| 241 | - | |
| 242 | - if ( ! empty( $request ) ) { | |
| 243 | - | |
| 244 | - foreach ( $this->sections as $section ) { | |
| 245 | - | |
| 246 | - if ( ! empty( $section['fields'] ) ) { | |
| 247 | - | |
| 248 | - foreach ( $section['fields'] as $field ) { | |
| 249 | - | |
| 250 | - if ( ! empty( $field['id'] ) ) { | |
| 251 | - | |
| 252 | - $field_id = $field['id']; | |
| 253 | - $field_value = isset( $request[$field_id] ) ? $request[$field_id] : ''; | |
| 254 | - | |
| 255 | - // Sanitize "post" request of field. | |
| 256 | - if ( ! isset( $field['sanitize'] ) ) { | |
| 257 | - | |
| 258 | - if( is_array( $field_value ) ) { | |
| 259 | - $data[$field_id] = wp_kses_post_deep( $field_value ); | |
| 260 | - } else { | |
| 261 | - $data[$field_id] = wp_kses_post( $field_value ); | |
| 262 | - } | |
| 263 | - | |
| 264 | - } else if( isset( $field['sanitize'] ) && is_callable( $field['sanitize'] ) ) { | |
| 265 | - | |
| 266 | - $data[$field_id] = call_user_func( $field['sanitize'], $field_value ); | |
| 267 | - | |
| 268 | - } else { | |
| 269 | - | |
| 270 | - // A sanitize callback was declared but is not callable; never store raw input. | |
| 271 | - if ( is_array( $field_value ) ) { | |
| 272 | - $data[$field_id] = wp_kses_post_deep( $field_value ); | |
| 273 | - } else { | |
| 274 | - $data[$field_id] = wp_kses_post( $field_value ); | |
| 275 | - } | |
| 276 | - | |
| 277 | - } | |
| 278 | - | |
| 279 | - // Validate "post" request of field. | |
| 280 | - if ( isset( $field['validate'] ) && is_callable( $field['validate'] ) ) { | |
| 281 | - | |
| 282 | - $has_validated = call_user_func( $field['validate'], $field_value ); | |
| 283 | - | |
| 284 | - if ( ! empty( $has_validated ) ) { | |
| 285 | - | |
| 286 | - $errors['sections'][$count] = true; | |
| 287 | - $errors['fields'][$field_id] = $has_validated; | |
| 288 | - $data[$field_id] = $this->get_meta_value( $comment_id, $field ); | |
| 289 | - | |
| 290 | - } | |
| 291 | - | |
| 292 | - } | |
| 293 | - | |
| 294 | - } | |
| 295 | - | |
| 296 | - } | |
| 297 | - | |
| 298 | - } | |
| 299 | - | |
| 300 | - $count++; | |
| 301 | - | |
| 302 | - } | |
| 303 | - | |
| 304 | - } | |
| 305 | - | |
| 306 | - $data = apply_filters( "adminify_{$this->unique}_save", $data, $comment_id, $this ); | |
| 307 | - | |
| 308 | - do_action( "adminify_{$this->unique}_save_before", $data, $comment_id, $this ); | |
| 309 | - | |
| 310 | - if ( empty( $data ) || ! empty( $request['_reset'] ) ) { | |
| 311 | - | |
| 312 | - if ( $this->args['data_type'] !== 'serialize' ) { | |
| 313 | - foreach ( $this->pre_fields as $field ) { | |
| 314 | - if ( ! empty( $field['id'] ) ) { | |
| 315 | - delete_comment_meta( $comment_id, $field['id'] ); | |
| 316 | - } | |
| 317 | - } | |
| 318 | - } else { | |
| 319 | - delete_comment_meta( $comment_id, $this->unique ); | |
| 320 | - } | |
| 321 | - | |
| 322 | - } else { | |
| 323 | - | |
| 324 | - if ( $this->args['data_type'] !== 'serialize' ) { | |
| 325 | - foreach ( $data as $key => $value ) { | |
| 326 | - update_comment_meta( $comment_id, $key, $value ); | |
| 327 | - } | |
| 328 | - } else { | |
| 329 | - update_comment_meta( $comment_id, $this->unique, $data ); | |
| 330 | - } | |
| 331 | - | |
| 332 | - if ( ! empty( $errors ) ) { | |
| 333 | - update_comment_meta( $comment_id, '_adminify_errors_'. $this->unique, $errors ); | |
| 334 | - } | |
| 335 | - | |
| 336 | - } | |
| 337 | - | |
| 338 | - do_action( "adminify_{$this->unique}_saved", $data, $comment_id, $this ); | |
| 339 | - | |
| 340 | - do_action( "adminify_{$this->unique}_save_after", $data, $comment_id, $this ); | |
| 341 | - | |
| 342 | - } | |
| 343 | - } | |
| 291 | + do_action( "adminify_{$this->unique}_save_after", $data, $comment_id, $this ); | |
| 292 | + } | |
| 293 | + } | |
| 344 | 294 | } |