'1', 'orderby' => 'nicename', 'fields' => array( 'user_login', 'display_name', ), ) ); $contributors = get_post_meta( $post->ID, 'ict101s_contributor', false ); wp_nonce_field( basename( __FILE__ ), 'ict_contributors_nonce' ); foreach ( self::$users as $author ) { ?> roles; foreach ( $users_roles as $key => $value ) { if ( ! in_array( $value, self::$allowed_roles, true ) ) { echo 'disabled'; break; } } ?> name="contributors[]" value="user_login ); ?>" $value ) { if ( $author->user_login === $value ) { echo ' checked'; } } } ?> > display_name ); ?>
post_type ); if ( ! current_user_can( $post_type->cap->edit_post, $post_id ) ) { return; } $new_meta_value = ( isset( $_POST['contributors'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['contributors'] ) ) : '' ); $current_meta_value = get_post_meta( $post_id, 'ict101s_contributor', false ); $meta_key = 'ict101s_contributor'; if ( '' !== $new_meta_value && ! empty( $current_meta_value ) ) { $diff_new_to_old = array_diff( $new_meta_value, $current_meta_value ); $diff_old_to_new = array_diff( $current_meta_value, $new_meta_value ); } if ( '' === $new_meta_value && empty( $current_meta_value ) ) { return; } elseif ( '' !== $new_meta_value && empty( $current_meta_value ) ) { self::save_meta_data( $new_meta_value, $meta_key, $post_id ); } elseif ( '' === $new_meta_value && ! empty( $current_meta_value ) ) { self::delete_meta_data( $current_meta_value, $meta_key, $post_id ); } elseif ( empty( $diff_new_to_old ) && empty( $diff_old_to_new ) ) { return; } elseif ( ! empty( $diff_new_to_old ) && empty( $diff_old_to_new ) ) { self::save_meta_data( $diff_new_to_old, $meta_key, $post_id ); } elseif ( empty( $diff_new_to_old ) && ! empty( $diff_old_to_new ) ) { self::delete_meta_data( $diff_old_to_new, $meta_key, $post_id ); } elseif ( ! empty( $diff_new_to_old ) && ! empty( $diff_old_to_new ) ) { self::save_meta_data( $diff_new_to_old, $meta_key, $post_id ); self::delete_meta_data( $diff_old_to_new, $meta_key, $post_id ); } } /** * Saves contributos' meta values in the post meta table * * Array of the value to save. * * @param array $array array of the contributors values to save * The name of the meta key to save. * * @param string $meta_key name of the meta key * ID of the current post. * * @param int $post_id id of current post. */ private static function save_meta_data( $array, $meta_key, $post_id ) { foreach ( $array as $key => $value ) { add_post_meta( $post_id, $meta_key, $value ); } } /** * Delets contributos' meta values in the post meta table * * Array of the value to delete. * * @param array $array array of the contributors values to delete * The name of the meta key to delete. * * @param string $meta_key name of the meta key * ID of the current post. * * @param int $post_id id of current post. */ private static function delete_meta_data( $array, $meta_key, $post_id ) { foreach ( $array as $key => $value ) { delete_post_meta( $post_id, $meta_key, $value ); } } }