lp__render_file_content( $file_path, $limit_args )

Reads the content of a file and returns it as a string with a line limit. You can specify whether to limit from the first or last lines and provide additional text to indicate truncated content.

Plugin

Namespace

Launchpad

Parameters

string

$file_path

The path of the file.

array

$limit_args

Array containing line, type, and text keys.

Return

string

The content of the file or an error message.

Source

File: \includes\lp-utility-functions.php

function lp__render_file_content( $file_path, $limit_args ) {
    if ( file_exists( $file_path ) ) {
        $content = file($file_path, FILE_IGNORE_NEW_LINES);
        if (is_array($content)) {

            $line_limit = lp__array_key_value( 'line', $limit_args );
            $limit_type = lp__array_key_value( 'type', $limit_args );
            $limit_text = lp__array_key_value( 'text', $limit_args );

            if ($limit_type === 'last') {
                $start_line = max(count($content) - $line_limit, 0);
            } else {
                $start_line = 0;
            }

            $limited_content = array_slice($content, $start_line, $line_limit);

            if (count($content) > $line_limit) {
                if( $limit_type === 'last' ){
                    $limited_content = array_merge([$limit_text],$limited_content);  
                } else {
                    $limited_content[] = $limit_text;
                }
            }

            return implode("\n", $limited_content);
        } else {
            return "Error reading file content.";
        }
    } else {
        return "File not found.";
    }
}

Related

Used to standardize file paths by replacing backslashes and removing duplicates.

Used to retrieve the normalized file path of a plugin file.

Used to retrieve and load plugin template based on slug and name.

Use to search for template files in theme and plugin directories.

Used to include Launchpad plugin files via require_once based on filename and plugin name.

Use to load multiple template parts based on slugs and names.