Jandon
Jandon

Reputation: 625

Avoid repeating 3 times the same function with a foreach

I'm trying to display 3 sort of posts on WordPress dashboard. All works fine but how can I to avoid to repeat 3 times the "same" function and WordPress hook, and use a foreach instead :

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 :

Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'add_recent_videos_dashboard' not found or invalid function name

$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 ?

Upvotes: 1

Views: 117

Answers (1)

A Haworth
A Haworth

Reputation: 36457

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.

Upvotes: 1

Related Questions