static function wpv_filter_custom_field_update_callback() { if ( ! current_user_can( 'manage_options' ) ) { $data = array( 'type' => 'capability', 'message' => __( 'You do not have permissions for that.', 'wpv-views' ) ); wp_send_json_error( $data ); } if ( ! isset( $_POST["wpnonce"] ) || ! wp_verify_nonce( $_POST["wpnonce"], 'wpv_view_filter_custom_field_nonce' ) ) { $data = array( 'type' => 'nonce', 'message' => __( 'Your security credentials have expired. Please reload the page to get new ones.', 'wpv-views' ) ); wp_send_json_error( $data ); } if ( ! isset( $_POST["id"] ) || ! is_numeric( $_POST["id"] ) || intval( $_POST['id'] ) < 1 ) { $data = array( 'type' => 'id', 'message' => __( 'Wrong or missing ID.', 'wpv-views' ) ); wp_send_json_error( $data ); } if ( empty( $_POST['filter_custom_fields'] ) ) { $data = array( 'type' => 'data_missing', 'message' => __( 'Wrong or missing data.', 'wpv-views' ) ); wp_send_json_error( $data ); } $change = false; $view_id = $_POST['id']; parse_str( $_POST['filter_custom_fields'], $filter_custom_fields ); $view_array = get_post_meta( $view_id, '_wpv_settings', true ); $summary = __( 'Select posts with custom field: ', 'wpv-views' ); $result = ''; foreach ( $filter_custom_fields as $filter_key => $filter_data ) { if ( ! isset( $view_array[$filter_key] ) || $filter_data != $view_array[$filter_key] ) { if ( is_array( $filter_data ) ) { $filter_data = array_map( 'sanitize_text_field', $filter_data ); $filter_data = array_map( array( 'WPV_Custom_Field_Filter', 'fix_lower_saving' ), $filter_data ); } else { $filter_data = sanitize_text_field( $filter_data ); $filter_data = WPV_Custom_Field_Filter::fix_lower_saving( $filter_data ); } $change = true; $view_array[$filter_key] = $filter_data; } } if ( ! isset( $view_array['custom_fields_relationship'] ) ) { $view_array['custom_fields_relationship'] = 'AND'; $change = true; } if ( $change ) { update_post_meta( $view_id, '_wpv_settings', $view_array ); do_action( 'wpv_action_wpv_save_item', $view_id ); } foreach ( array_keys( $view_array ) as $key ) { if ( strpos( $key, 'custom-field-' ) === 0 && strpos( $key, '_compare' ) === strlen( $key ) - strlen( '_compare' ) ) { $name = substr( $key, 0, strlen( $key ) - strlen( '_compare' ) ); if ( $result != '' ) { if ( $view_array['custom_fields_relationship'] == 'OR' ) { $result .= __( ' OR', 'wpv-views' ); } else { $result .= __( ' AND', 'wpv-views' ); } } $result .= wpv_get_custom_field_summary( $name, $view_array ); } } $summary .= $result; $data = array( 'id' => $view_id, 'message' => __( 'Custom field filter saved', 'wpv-views' ), 'summary' => $summary ); wp_send_json_success( $data ); }
/** * wpv_get_filter_custom_field_summary_txt * * Returns the custom fields filter summary for a View * * @param $view_settings * * @returns (string) $summary * * @since unknown */ function wpv_get_filter_custom_field_summary_txt( $view_settings ) { $result = ''; if( isset( $view_settings['query_type'] ) && $view_settings['query_type'][0] == 'posts' ) { $count = 0; foreach ( array_keys( $view_settings ) as $key ) { if ( strpos( $key, 'custom-field-' ) === 0 && strpos( $key, '_compare' ) === strlen( $key ) - strlen( '_compare' ) ) { $name = substr( $key, 0, strlen( $key ) - strlen( '_compare' ) ); $count++; if ( $result != '' ) { if ( isset( $view_settings['custom_fields_relationship'] ) && $view_settings['custom_fields_relationship'] == 'OR' ) { $result .= __( ' OR', 'wpv-views' ); } else { $result .= __( ' AND', 'wpv-views' ); } } $result .= wpv_get_custom_field_summary( $name, $view_settings ); } } } return $result; }