Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto cleanup values of “post” field when a post is deleted #1581

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 55 additions & 5 deletions inc/field.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public static function show( array $field, bool $saved, $post_id = 0 ) {
$meta = self::call( $field, 'meta', $post_id, $saved );
$meta = self::filter( 'field_meta', $meta, $field, $saved );

$meta = self::remove_options_deleted( (array) $meta, $field );
$meta = self::array_filter_recursive( $meta );

$begin = static::begin_html( $field );
$begin = self::filter( 'begin_html', $begin, $field, $meta );

Expand Down Expand Up @@ -58,6 +61,53 @@ public static function show( array $field, bool $saved, $post_id = 0 ) {
echo $outer_html; // phpcs:ignore WordPress.Security.EscapeOutput
}

/**
* remove_options_deleted
* @param array $meta Meta value.
* @param array $field Field parameters.
* @return array
*/
private static function remove_options_deleted( array $meta, array $field ): array {
if ( ! isset( $field['options'] ) || empty( $meta ) ) {
return $meta;
}

$options = array_keys( $field['options'] );
array_walk_recursive($meta, function ( &$value ) use ( $options ) {
if ( ! empty( $value ) && ! in_array( $value, $options ) ) {
$value = null;
}
});

return array_filter( $meta );
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nên đưa hàm này vào class của các field object (mình có class object choice rồi).


/**
* array_filter_recursive
* @link https://gist.github.com/benjamw/1690140
* @param array
* @param string optional callback function name
* @param bool optional flag removal of empty arrays after filtering
* @return array merged array
*/
private static function array_filter_recursive( array $array, callable $callback = null, bool $remove_empty_arrays = false ): array {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hàm này đưa vào class Helpers\Arr.

foreach ( $array as $key => & $value ) { // mind the reference
if ( is_array( $value ) ) {
$value = call_user_func_array( [ 'self', 'array_filter_recursive' ], array( $value, $callback, $remove_empty_arrays ) );
if ( $remove_empty_arrays && ! (bool) $value ) {
unset( $array[ $key ] );
}
} elseif ( ! is_null( $callback ) && ! $callback( $value ) ) {
unset( $array[ $key ] );
} elseif ( ! (bool) $value ) {
unset( $array[ $key ] );
}
}
unset( $value ); // kill the reference

return array_filter( $array );
}

/**
* Get field HTML.
*
Expand All @@ -70,7 +120,7 @@ public static function html( $meta, $field ) {
return '';
}

protected static function begin_html( array $field ) : string {
protected static function begin_html( array $field ): string {
$id = $field['attributes']['id'] ?? $field['id'];
$required = $field['required'] || ! empty( $field['attributes']['required'] );

Expand Down Expand Up @@ -101,16 +151,16 @@ protected static function begin_html( array $field ) : string {
return $label . $input_open;
}

protected static function end_html( array $field ) : string {
protected static function end_html( array $field ): string {
return RWMB_Clone::add_clone_button( $field ) . static::input_description( $field ) . '</div>';
}

protected static function label_description( array $field ) : string {
protected static function label_description( array $field ): string {
$id = $field['id'] ? ' id="' . esc_attr( $field['id'] ) . '-label-description"' : '';
return $field['label_description'] ? "<p{$id} class='description'>{$field['label_description']}</p>" : '';
}

protected static function input_description( array $field ) : string {
protected static function input_description( array $field ): string {
$id = $field['id'] ? ' id="' . esc_attr( $field['id'] ) . '-description"' : '';
return $field['desc'] ? "<p{$id} class='description'>{$field['desc']}</p>" : '';
}
Expand Down Expand Up @@ -369,7 +419,7 @@ public static function get_attributes( $field, $value = null ) {
return $attributes;
}

public static function render_attributes( array $attributes ) : string {
public static function render_attributes( array $attributes ): string {
$output = '';

$attributes = array_filter( $attributes, 'RWMB_Helpers_Value::is_valid_for_attribute' );
Expand Down