Пример #1
0
/**
 * Displays a human readable HTML representation of the difference between two strings.
 * similar to wp_text_diff, but tracks and returns could of lines added and removed
 *
 * @since 3.6.0
 *
 * @see wp_parse_args() Used to change defaults to user defined settings.
 * @uses Text_Diff
 * @uses WP_Text_Diff_Renderer_Table
 *
 * @param string $left_string "old" (left) version of string
 * @param string $right_string "new" (right) version of string
 * @param string|array $args Optional. Change 'title', 'title_left', and 'title_right' defaults.
 * @return array contains html, linesadded & linesdeletd, empty string if strings are equivalent.
 */
function wp_text_diff_with_count($left_string, $right_string, $args = null)
{
    $defaults = array('title' => '', 'title_left' => '', 'title_right' => '');
    $args = wp_parse_args($args, $defaults);
    if (!class_exists('WP_Text_Diff_Renderer_Table')) {
        require ABSPATH . WPINC . '/wp-diff.php';
    }
    $left_string = normalize_whitespace($left_string);
    $right_string = normalize_whitespace($right_string);
    $left_lines = explode("\n", $left_string);
    $right_lines = explode("\n", $right_string);
    $text_diff = new Text_Diff($left_lines, $right_lines);
    $lines_added = $text_diff->countAddedLines();
    $lines_deleted = $text_diff->countDeletedLines();
    $renderer = new WP_Text_Diff_Renderer_Table();
    $diff = $renderer->render($text_diff);
    if (!$diff) {
        return '';
    }
    $r = "<table class='diff'>\n";
    if (!empty($args['show_split_view'])) {
        $r .= "<col class='content diffsplit left' /><col class='content diffsplit middle' /><col class='content diffsplit right' />";
    } else {
        $r .= "<col class='content' />";
    }
    if ($args['title'] || $args['title_left'] || $args['title_right']) {
        $r .= "<thead>";
    }
    if ($args['title']) {
        $r .= "<tr class='diff-title'><th colspan='4'>{$args['title']}</th></tr>\n";
    }
    if ($args['title_left'] || $args['title_right']) {
        $r .= "<tr class='diff-sub-title'>\n";
        $r .= "\t<td></td><th>{$args['title_left']}</th>\n";
        $r .= "\t<td></td><th>{$args['title_right']}</th>\n";
        $r .= "</tr>\n";
    }
    if ($args['title'] || $args['title_left'] || $args['title_right']) {
        $r .= "</thead>\n";
    }
    $r .= "<tbody>\n{$diff}\n</tbody>\n";
    $r .= "</table>";
    return array('html' => $r, 'lines_added' => $lines_added, 'lines_deleted' => $lines_deleted);
}
Пример #2
0
 function check_filterless_things()
 {
     // Var is string with length 113: /wp-admin/plugin-editor.php?file=my-plugin%2Fviews%2Fplugin-file.php
     $referer = wp_get_referer();
     // contains key "path" with value like "/wp-admin/plugin-editor.php"
     $referer_info = parse_url($referer);
     if ("/wp-admin/plugin-editor.php" === $referer_info["path"]) {
         // We are in plugin editor
         // Check for plugin edit saved
         if (isset($_POST["newcontent"]) && isset($_POST["action"]) && "update" == $_POST["action"] && isset($_POST["file"]) && !empty($_POST["file"])) {
             // A file was edited
             $file = $_POST["file"];
             // $plugins = get_plugins();
             // http://codex.wordpress.org/Function_Reference/wp_text_diff
             // Generate a diff of changes
             if (!class_exists('WP_Text_Diff_Renderer_Table')) {
                 require_once ABSPATH . WPINC . '/wp-diff.php';
             }
             $original_file_contents = file_get_contents(WP_PLUGIN_DIR . "/" . $file);
             $new_file_contents = wp_unslash($_POST["newcontent"]);
             $left_lines = explode("\n", $original_file_contents);
             $right_lines = explode("\n", $new_file_contents);
             $text_diff = new Text_Diff($left_lines, $right_lines);
             $num_added_lines = $text_diff->countAddedLines();
             $num_removed_lines = $text_diff->countDeletedLines();
             // Generate a diff in classic diff format
             $renderer = new Text_Diff_Renderer();
             $diff = $renderer->render($text_diff);
             $this->infoMessage('plugin_file_edited', array("plugin_edited_file" => $file, "plugin_edit_diff" => $diff, "plugin_edit_num_added_lines" => $num_added_lines, "plugin_edit_num_removed_lines" => $num_removed_lines));
             $did_log = true;
         }
     }
 }
Пример #3
0
 /**
  * Get the difference between original text and a changed body of text
  *
  * @param string The old text
  * @param string The changed text
  * @return array 
  */
 public static function get_text_diff($old_content, $new_content)
 {
     $old_file = explode("\n", $old_content);
     $new_file = explode("\n", $new_content);
     $diff = new Text_Diff('auto', array($old_file, $new_file));
     $added = $diff->countAddedLines();
     $deleted = $diff->countDeletedLines();
     if ($added == 0 && $deleted == 0) {
         return false;
     }
     $net = $added - $deleted;
     $result = array();
     $result_added = 0;
     $result_changed = 0;
     $result_deleted = 0;
     if ($net == 0) {
         $result_changed = $added;
     } elseif ($net > 0) {
         $result_changed = $added - $net;
         $result_added = $net;
     } elseif ($net < 0) {
         $result_changed = $deleted - abs($net);
         $result_deleted = abs($net);
     }
     return array('added' => $result_added, 'deleted' => $result_deleted, 'changed' => $result_changed);
 }