OOP Switch statement with array as parameter

Question

So I’m having some issues with a switch statement when applying it to filters, when I define the case string, it applied it to both columns that I have:

By me defining ‘authors’, it pulls in overwrites the ‘Authors’ and ‘Types’ columns, how would I be able to define it as such Column:get_columns('authors', 'recipe-types') and it will pull both the authors case and recipe-types?

Here is the filters:

add_action('manage_recipe_posts_custom_column', function ($column) {
    Column::get_columns('authors');
}, 10, 3);

add_filter('manage_edit-recipe_columns', function($columns) {
    unset($columns['author']);

    $columns = [
        'cb' => '<input type="checkbox" />',
        'title' => __('Title'),
        'authors' => __('Authors'),
        'recipe-types' => __('Types'),
        'tags' => __('Tags'),
        'categories' => __('Categories'),
        'date' => __('Date'),
    ];
    return $columns;
});

// Add 'Authors' column to display all authors
add_filter('manage_recipe_posts_columns', function($columns) {
    return array_merge($columns, [
        'authors' => __('Authors'),
    ]);
});

Here is an example (Overwrites both columns with same results, not just the ‘Authors’ column):
enter image description here

Here is the class that I’ve created:

class Column
{
    public static function get_columns($column = '')
    {
        switch ($column) {
            case 'authors':
                $recipe = Recipe::init($id);

                if (is_array($recipe) || is_object($recipe)) {
                    $authors = $recipe->get_authors();

                    if (isset($authors) && !empty($authors)) {
                        $arr = [];
                        foreach ($authors as $profile) {
                            $arr[] = '<a href="' . $profile->get_url() . '">' . $profile->get_name() . '</a>';
                        }
                        echo implode(', ', $arr);
                        break;
                    }
                    echo "Blank";
                    break;
                } else {
                    echo 'Blank';
                }
                break;

            case 'recipe-types':

                $categories = get_the_term_list('', 'recipe-types', '', ', ',
                    '');

                if (!empty($categories)) {
                    echo $categories;
                } else {
                    echo '&ndash;';
                }

                break;
        }

        return $column;
    }
}
0
, Sem 4 years 2020-02-28T08:38:20-05:00 0 Answers 64 views 0

Leave an answer

Browse
Browse