Making WordPress.org


Ignore:
Timestamp:
06/05/2014 09:42:29 PM (12 years ago)
Author:
coffee2code
Message:

Code Reference: Add inline source code display for functions and methods. props atimmer. See #176

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/inc/template-tags.php

    r640 r676  
    639639        }
    640640
     641        /**
     642         * Retrieve source code for a function
     643         *
     644         * @param int $post_id
     645         *
     646         * @return string The sourc
     647         */
     648        function get_source_code( $post_id = null ) {
     649
     650                if ( empty( $post_id ) ) {
     651                        $post_id = get_the_ID();
     652                }
     653
     654                // Get the total file sourcecode.
     655                $source_file = get_source_file( $post_id );
     656
     657                // Put the total source code in an array.
     658                $total_source_code = file_get_contents( ABSPATH . $source_file );
     659                $total_source_code = explode( "\n", $total_source_code );
     660
     661                // Get the start and end lines.
     662                $start_line = get_post_meta( $post_id, '_wp-parser_line_num', true ) - 1;
     663                $end_line =   get_post_meta( $post_id, '_wp-parser_end_line_num', true );
     664
     665                // Get the correct source code.
     666                $source_code = array_slice( $total_source_code, $start_line, $end_line - $start_line );
     667
     668                return implode( "\n", $source_code );
     669        }
     670
    641671}
Note: See TracChangeset for help on using the changeset viewer.