Plugin to list files in CCAP directory

Hello, I got my site with S2Member and File Away hacked and I search for an alternative to File Away - something that lists all files in a CCAP directory. I found some plugins listing files in directories but not specifically in S2Member context. Will they work ?

I’m not sure, you could try…

I went ahead and wrote a shortcode for you… I may polish it and publish it in the future, so let me know if you need it to do more things, so I can take them into account.

Create this file: /wp-content/mu-plugins/s2-list-files.php

<?php

function s2_list_files_shortcode($atts) {
    // Extract the optional 'access' attribute
    $atts = shortcode_atts(array(
        'access' => '',
    ), $atts, 's2-list-files');

    $access_raw = sanitize_text_field($atts['access']);
    $base_path = WP_CONTENT_DIR . '/plugins/s2member-files/';
    $s2_path_prefix = '';

    if (!empty($access_raw)) {
        // Determine access type
        if (stripos($access_raw, 'level') === 0) {
            // Example: level1 → access-s2member-level1
            $s2_path_prefix = 'access-s2member-' . sanitize_file_name($access_raw) . '/';
        } else {
            // Example: music → access-s2member-ccap-music
            $s2_path_prefix = 'access-s2member-ccap-' . sanitize_file_name($access_raw) . '/';
        }

        $base_path .= $s2_path_prefix;
    }

    // Ensure the directory exists
    if (!is_dir($base_path)) {
        return '<p><em>Directory not found: ' . esc_html($s2_path_prefix) . '</em></p>';
    }

    // Get files in the directory, skip .htaccess
    $files = scandir($base_path);
    $files = array_filter($files, function($file) use ($base_path) {
        return is_file($base_path . $file) && $file !== '.htaccess';
    });

    if (empty($files)) {
        return '<p><em>No files found.</em></p>';
    }

    // Build output list with S2Member download links
    $output = '<ul>';
    foreach ($files as $file) {
        $s2_path = $s2_path_prefix . $file;  // prefix might be empty if no access was given
        $download_url = esc_url(site_url('?s2member_file_download=' . rawurlencode($s2_path)));
        $output .= '<li><a href="' . $download_url . '" target="_blank">' . esc_html($file) . '</a></li>';
    }
    $output .= '</ul>';

    return $output;
}
add_shortcode('s2-list-files', 's2_list_files_shortcode');

And then you can use it on a page like this:

[s2-list-files /] Lists /s2member-files/ (root only)
[s2-list-files access="level1" /] Lists /s2member-files/access-s2member-level1/
[s2-list-files access="gold" /] Lists /s2member-files/access-s2member-ccap-gold/

:slight_smile:

Oh, very cool, thanks a lot. I will try this and give my needs if needed. Thanks a lot.

1 Like

It works nicely. I have to style it a bit but it’s nice. The only thing I need is to show the date of the upload.

1 Like

Great!

Here’s one with the upload time next to the filename:

<?php

function s2_list_files_shortcode($atts) {
    // Extract optional 'access' attribute
    $atts = shortcode_atts(array(
        'access' => '',
    ), $atts, 's2-list-files');

    $access_raw = sanitize_text_field($atts['access']);
    $base_path = WP_CONTENT_DIR . '/plugins/s2member-files/';
    $s2_path_prefix = '';

    // Determine subdirectory
    if (!empty($access_raw)) {
        if (stripos($access_raw, 'level') === 0) {
            $s2_path_prefix = 'access-s2member-' . sanitize_file_name($access_raw) . '/';
        } else {
            $s2_path_prefix = 'access-s2member-ccap-' . sanitize_file_name($access_raw) . '/';
        }
        $base_path .= $s2_path_prefix;
    }

    if (!is_dir($base_path)) {
        return '<p><em>Directory not found: ' . esc_html($s2_path_prefix) . '</em></p>';
    }

    $files = scandir($base_path);
    $files = array_filter($files, function($file) use ($base_path) {
        return is_file($base_path . $file) && $file !== '.htaccess';
    });

    if (empty($files)) {
        return '<p><em>No files found.</em></p>';
    }

    $output = '<ul>';
    foreach ($files as $file) {
        $file_path = $base_path . $file;
        $modified = filemtime($file_path);
        $formatted_time = date_i18n('F j, Y, g:i a', $modified);

        $s2_path = $s2_path_prefix . $file;
        $download_url = esc_url(site_url('?s2member_file_download=' . rawurlencode($s2_path)));

        $output .= '<li><a href="' . $download_url . '" target="_blank">'
                 . esc_html($file) . '</a> <em>(' . esc_html($formatted_time) . ')</em></li>';
    }
    $output .= '</ul>';

    return $output;
}
add_shortcode('s2-list-files', 's2_list_files_shortcode');

:slight_smile:

Thanks again. I will change the format of the date as I’m French. It may be interesting for the users to adapt date format to the WP global settings.

1 Like
`$formatted_time = date_i18n(get_option('date_format') . ', ' . get_option('time_format'), $modified );`

:slight_smile:

1 Like

Perfect, thanks.

1 Like