これが持つコード行の量を減らす (最適化する) 関数があります。この関数には、繰り返されるいくつかのフラグメントがあります。
ラムダ関数を使用してこのコードを削減できますか?
これは私の関数コードです:
static function get_all_category_with_widgets( $status = 'all' ) {
$all_categories = SB_Central::get_categories();
$all_widgets = SB_Settings::get_sb_widgets(); // Get all widgets from options variable.
foreach ( $all_categories as $category_key => $category_value ) {
foreach ( $all_widgets as $widget_value ) {
// Create one widget
$widget = array_merge($widget_value['widget'], array ( 'id' => $widget_value['id'], 'status' => $widget_value['status']));
// In this case save active and disable.
if ( $status == 'active_and_disable' && ( $widget_value['status'] == 'active' || $widget_value['status'] == 'disable' )) {
if ( $category_value[ 'category_title' ] == $widget_value[ 'category' ][ 'title' ] ) {
$all_categories[ $category_key ][ 'widgets' ][ ] = $widget; // Save widget
}
} elseif ( $status == 'active' && $widget_value['status'] == 'active' ) {
if ( $category_value[ 'category_title' ] == $widget_value[ 'category' ][ 'title' ] ) {
$all_categories[ $category_key ][ 'widgets' ][ ] = $widget; // Save widget
}
} elseif ( $status == 'disable' && $widget_value['status'] == 'disable' ) {
if ( $category_value[ 'category_title' ] == $widget_value[ 'category' ][ 'title' ] ) {
$all_categories[ $category_key ][ 'widgets' ][ ] = $widget; // Save widget
}
} elseif ( $status == 'deleted' && $widget_value['status'] == 'deleted' ) {
if ( $category_value[ 'category_title' ] == $widget_value[ 'category' ][ 'title' ] ) {
$all_categories[ $category_key ][ 'widgets' ][ ] = $widget; // Save widget
}
} elseif ( $status == 'all' ) {
if ( $category_value[ 'category_title' ] == $widget_value[ 'category' ][ 'title' ] ) {
$all_categories[ $category_key ][ 'widgets' ][ ] = $widget; // Save widget
}
}
}
}
return $all_categories;
}
そして、これは常に私が繰り返しているコードです:
if ( $category_value[ 'category_title' ] == $widget_value[ 'category' ][ 'title' ] ) {
$all_categories[ $category_key ][ 'widgets' ][ ] = $widget; // Save widget
}
このコードを減らすためのより良い方法を知っていれば、歓迎されます。