最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

functions - OOP Switch statement with array as parameter

programmeradmin1浏览0评论

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):

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;
    }
}

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):

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;
    }
}
Share Improve this question edited Feb 28, 2020 at 2:06 DevSem asked Feb 27, 2020 at 22:01 DevSemDevSem 2092 silver badges11 bronze badges 2
  • Where is the code that registers/adds the columns? Are there other methods in your column class? It isn't OO if you're just creating a class with static methods. I'm also not sure if your get_columns is trying to return the values or echo them out. Any clarification you can add would be helpful! – Tom J Nowell Commented Feb 27, 2020 at 22:25
  • @TomJNowell, I've updated the two additional filters, where it sorts the columns and then add in a 'authors' column. – DevSem Commented Feb 28, 2020 at 2:07
Add a comment  | 

1 Answer 1

Reset to default 0

It always shows authors because you always output the authors column, even if $column has a different value.

Let's take the filter and substitute $column for its string value to demonstrate.

First, WP wants the output for the authors column:

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

Next, it calls the action again, but for the recipe-types column:

add_action('manage_recipe_posts_custom_column', function ('recipe-types') {
    Column::get_columns('authors');
}, 10, 3);

Notice that even though the action tells you which column it wants to output, that value is ignored. Also, notice that the action gets called multiple times with different column values. Think of actions like events, an event can happen more than once.

What's more, the static method Column::get_columns will take an arbitrary column name and generate the correct response, or at least attempt to. The switch statement is fine, but it always receives the same value, this problem has nothing to do with classes, objects, or switch statements, and would have happened even if it was just a function call.

So, instead of hardcoding it to authors and wondering why it always shows the authors, pass in the column variable instead.

Which brings me to your Column class. This isn't OOP, it's just a single function turned into a static method of a class. I recommend removing the Column class and just using a function. For it to be object-oriented programming I would expect to be able to create a Column object, and pass in parameters, and have the object takes care of registering everything on its own, without static methods, or needing inheritance for every column, with a Column object created for each custom column. Such an implementation would be over-engineering. Just use a function as you do elsewhere.

Additionally, it should echo its output, the return statements are incorrect.

发布评论

评论列表(0)

  1. 暂无评论