简体   繁体   English

避免使用 foreach 重复 3 次相同的函数

[英]Avoid repeating 3 times the same function with a foreach

I'm trying to display 3 sort of posts on WordPress dashboard.我正在尝试在 WordPress 仪表板上显示 3 种帖子。 All works fine but how can I to avoid to repeat 3 times the "same" function and WordPress hook, and use a foreach instead :一切正常,但我怎样才能避免重复“相同”函数和 WordPress 钩子 3 次,并使用foreach代替:

function recent_videos_dashboard() {
?>
   <ul>
     <?php
        global $post;

        $posts = get_posts(array(
            'post_type' => 'videos',
            'numberposts' => 5
        ));

        foreach( $posts as $post ) :  setup_postdata($post); ?>
            <li><a href="<?php echo admin_url( 'post.php?post=' . $post->ID ) . '&action=edit'; ?>"><?php echo date('j/m/Y', strtotime($post->post_date)); ?> - <?php echo $post->post_title; ?></a></li>
        <?php endforeach; ?>
  </ul>
<?php
}

function add_recent_videos_dashboard() {
    wp_add_dashboard_widget( 'recent_videos_dashboard', __( 'Vidéo(s) récente(s)' ), 'recent_videos_dashboard' );
}

if ( !current_user_can( 'manage_options' ) ) {
    add_action('wp_dashboard_setup', 'add_recent_videos_dashboard' );
}

function recent_posts_dashboard() {
?>
   <ul>
     <?php
        global $post;

        $posts = get_posts(array(
            'post_type' => 'post',
            'numberposts' => 5
        ));

        foreach( $posts as $post ) :  setup_postdata($post); ?>
            <li><a href="<?php echo admin_url( 'post.php?post=' . $post->ID ) . '&action=edit'; ?>"><?php echo date('j/m/Y', strtotime($post->post_date)); ?> - <?php echo $post->post_title; ?></a></li>
        <?php endforeach; ?>
  </ul>
<?php
}

function add_recent_posts_dashboard() {
    wp_add_dashboard_widget( 'recent_posts_dashboard', __( 'Article(s) récente(s)' ), 'recent_posts_dashboard' );
}

if ( !current_user_can( 'manage_options' ) ) {
    add_action('wp_dashboard_setup', 'add_recent_posts_dashboard' );
}

function recent_podcasts_dashboard() {
?>
   <ul>
     <?php
        global $post;

        $posts = get_posts(array(
            'post_type' => 'podcasts',
            'numberposts' => 5
        ));

        foreach( $posts as $post ) :  setup_postdata($post); ?>
            <li><a href="<?php echo admin_url( 'post.php?post=' . $post->ID ) . '&action=edit'; ?>"><?php echo date('j/m/Y', strtotime($post->post_date)); ?> - <?php echo $post->post_title; ?></a></li>
        <?php endforeach; ?>
  </ul>
<?php
}

function add_recent_podcasts_dashboard() {
    wp_add_dashboard_widget( 'recent_podcasts_dashboard', __( 'Podcast(s) récente(s)' ), 'recent_podcasts_dashboard' );
}

if ( !current_user_can( 'manage_options' ) ) {
    add_action('wp_dashboard_setup', 'add_recent_podcasts_dashboard' );
}

So I used a foreach instead but I got an error :所以我改用了foreach但我得到了一个错误:

Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'add_recent_videos_dashboard' not found or invalid function name警告:call_user_func_array() 期望参数 1 为有效回调,未找到函数“add_recent_videos_dashboard”或无效的函数名称

$array_post_types = array('post','videos','podcasts');

foreach ($array_post_types as $array_post_type) {
    $recent_post = 'recent_' . $array_post_type . '_dashboard';

    ${$recent_post} = function() use ($array_post_type) {
        global $post;

        ${'array_' .$array_post_type} = get_posts(array(
            'post_type' => $array_post_type,
            'numberposts' => 5
        ));

        foreach( ${'array_' .$array_post_type} as $post ) :  setup_postdata($post); ?>
        <li><a href="<?php echo admin_url( 'post.php?post=' . $post->ID ) . '&action=edit'; ?>"><?php echo date('j/m/Y', strtotime($post->post_date)); ?> - <?php echo $post->post_title; ?></a></li>
        <?php endforeach;
    };

    $add_recent_posts = 'add_recent_' . $array_post_type . '_dashboard';

    ${$add_recent_posts} = function () use ($array_post_type) {
        wp_add_dashboard_widget( $recent_post, __( 'Vidéo(s) récente(s)' ), $recent_post );
    };

    if ( !current_user_can( 'manage_options' ) ) {
        add_action('wp_dashboard_setup', $add_recent_posts );
    }
}

Can I have some help please ?我可以帮忙吗?

Taking the given code but changing to use anonymous functions and a slightly different array of post-types (to include the name string for each so we can use a different one for each post type) here is an attempt at the code.使用给定的代码但更改为使用匿名函数和稍微不同的帖子类型数组(包括每个帖子类型的名称字符串,以便我们可以为每种帖子类型使用不同的名称),这是对代码的一种尝试。 I do not have a way of testing this so please use with caution - and let me know what I have wrong:我没有办法测试这个,所以请谨慎使用 - 让我知道我有什么问题:

// do stuff here only if it is an 'ordinary' sort of user
if ( !current_user_can( 'manage_options' ) ) :

  $my_posts = [
    [ 'type' => 'post', 'name' => 'Article(s) récente(s)'],
    [ 'type' => 'videos', 'name' => 'Vidéo(s) récente(s)'],
    [ 'type' => 'podcasts', 'name' => 'Podcast(s) récente(s)']
  ];

  foreach ( $my_posts as $my_post ) :

    add_action( 'wp_dashboard_setup', function() {
      wp_add_dashboard_widget('recent_' . $my_post['type'] . '_dashboard', __( $my_post['name'] ), function () {    
?>
   <ul>
<?php
        global $post;

        $posts = get_posts(array(
            'post_type' => $my_post['type'],
            'numberposts' => 5
        ));

        foreach( $posts as $post ) :  setup_postdata($post); ?>
            <li><a href="<?php echo admin_url( 'post.php?post=' . $post->ID ) . '&action=edit'; ?>"><?php echo date('j/m/Y', strtotime($post->post_date)); ?> - <?php echo $post->post_title; ?></a></li>
<?php   endforeach; ?>
   </ul>
<?php       
        }//end of callback function given to wp_add_dashboard_widget call
      );// end of call to wp-add-dashboard_widget
    } //end of callback function given to add_action call
    
    );//end of add_action
    
  endforeach;//end of going through each type of my_posts
  
endif;//finished doing stuff for ordinary user

Note: anonymous functions are used to avoid cluttering up the name space.注意:匿名函数用于避免混淆命名空间。 Also my_ is put as a prefix - it could be changed to some unique prefix for your function/plugin so as to ensure no clash with other code. my_ 也作为前缀放置 - 它可以更改为您的函数/插件的某些唯一前缀,以确保与其他代码不发生冲突。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM