Changeset 677 for sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/inc/template-tags.php
- Timestamp:
- 06/05/2014 09:46:52 PM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/inc/template-tags.php
r676 r677 640 640 641 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 ) { 642 * Does the post type have source code? 643 * 644 * @param string Optional. The post type name. If blank, assumes current post type. 645 * 646 * @return boolean 647 */ 648 function post_type_has_source_code( $post_type = null ) { 649 $post_type = $post_type ? $post_type : get_post_type(); 650 $post_types_with_source_code = array( 'wp-parser-method', 'wp-parser-function' ); 651 652 return in_array( $post_type, $post_types_with_source_code ); 653 } 654 655 /** 656 * Retrieve the root directory of the parsed WP code. 657 * 658 * If the option 'wp_parser_root_import_dir' (as set by the parser) is not 659 * set, then assume ABSPATH. 660 * 661 * @return string 662 */ 663 function get_source_code_root_dir() { 664 $root_dir = get_option( 'wp_parser_root_import_dir' ); 665 666 return $root_dir ? trailingslashit( $root_dir ) : ABSPATH; 667 } 668 669 /** 670 * Retrieve source code for a function or method. 671 * 672 * @param int $post_id Optional. The post ID. 673 * @param bool $force_parse Optional. Ignore potential value in post meta and reparse source file for source code? 674 * 675 * @return string The source code. 676 */ 677 function get_source_code( $post_id = null, $force_parse = false ) { 649 678 650 679 if ( empty( $post_id ) ) { … … 652 681 } 653 682 654 // Get the total file sourcecode. 683 // Get the source code stored in post meta. 684 $meta_key = '_wp-parser_source_code'; 685 if ( ! $force_parse && $source_code = get_post_meta( $post_id, $meta_key, true ) ) { 686 return $source_code; 687 } 688 689 /* Source code hasn't been stored in post meta, so parse source file to get it. */ 690 691 // Get the name of the source file. 655 692 $source_file = get_source_file( $post_id ); 656 693 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 694 // 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 ); 695 $start_line = intval( get_post_meta( $post_id, '_wp-parser_line_num', true ) ) - 1; 696 $end_line = intval( get_post_meta( $post_id, '_wp-parser_end_line_num', true ) ); 697 698 // Sanity check to ensure proper conditions exist for parsing 699 if ( ! $source_file || ! $start_line || ! $end_line || ( $start_line > $end_line ) ) { 700 return ''; 701 } 702 703 // Find just the relevant source code 704 $source_code = ''; 705 $handle = @fopen( get_source_code_root_dir() . $source_file, 'r' ); 706 if ( $handle ) { 707 $line = -1; 708 while ( ! feof( $handle ) ) { 709 $line++; 710 $source_line = fgets( $handle ); 711 if ( $line > $end_line ) { 712 break; 713 } 714 if ( $line < $start_line ) { 715 continue; 716 } 717 $source_code .= $source_line; 718 } 719 fclose( $handle ); 720 } 721 722 update_post_meta( $post_id, $meta_key, $source_code ); 723 724 return $source_code; 669 725 } 670 726
Note: See TracChangeset
for help on using the changeset viewer.