<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[2186] 2013/ahoereth/trunk: Renaming files to represent the classes they contain.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://gsoc.trac.wordpress.org/changeset/2186">2186</a></dd>
<dt>Author</dt> <dd>a.hoereth</dd>
<dt>Date</dt> <dd>2013-08-02 16:05:41 +0000 (Fri, 02 Aug 2013)</dd>
</dl>
<h3>Log Message</h3>
<pre>Renaming files to represent the classes they contain. See <a href="http://gsoc.trac.wordpress.org/ticket/325">#325</a></pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#2013ahoerethtrunkcoderevisionsphp">2013/ahoereth/trunk/code-revisions.php</a></li>
</ul>
<h3>Added Paths</h3>
<ul>
<li><a href="#2013ahoerethtrunkincclasscoderevisionseditorsphp">2013/ahoereth/trunk/inc/class-code-revisions-editors.php</a></li>
<li><a href="#2013ahoerethtrunkincclasscoderevisionsphp">2013/ahoereth/trunk/inc/class-code-revisions.php</a></li>
</ul>
<h3>Removed Paths</h3>
<ul>
<li><a href="#2013ahoerethtrunkinceditorsphp">2013/ahoereth/trunk/inc/editors.php</a></li>
<li><a href="#2013ahoerethtrunkincmainphp">2013/ahoereth/trunk/inc/main.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="2013ahoerethtrunkcoderevisionsphp"></a>
<div class="modfile"><h4>Modified: 2013/ahoereth/trunk/code-revisions.php (2185 => 2186)</h4>
<pre class="diff"><span>
<span class="info">--- 2013/ahoereth/trunk/code-revisions.php 2013-08-01 23:53:09 UTC (rev 2185)
+++ 2013/ahoereth/trunk/code-revisions.php 2013-08-02 16:05:41 UTC (rev 2186)
</span><span class="lines">@@ -40,7 +40,7 @@
</span><span class="cx"> * Initialize the main class - required on all pages. It registers the custom
</span><span class="cx"> * post type etc.
</span><span class="cx"> */
</span><del>-include_once( CODE_REVISIONS_DIR . 'inc/main.php' );
</del><ins>+include_once( CODE_REVISIONS_DIR . 'inc/class-code-revisions.php' );
</ins><span class="cx"> new Code_Revisions;
</span><span class="cx">
</span><span class="cx"> /*
</span><span class="lines">@@ -51,6 +51,6 @@
</span><span class="cx"> */
</span><span class="cx"> global $pagenow;
</span><span class="cx"> if ( in_array( $pagenow, array( 'theme-editor.php', 'plugin-editor.php' ) ) ) {
</span><del>- include_once( CODE_REVISIONS_DIR . 'inc/editors.php' );
</del><ins>+ include_once( CODE_REVISIONS_DIR . 'inc/class-code-revisions-editors.php' );
</ins><span class="cx"> new Code_Revisions_Editors;
</span><span class="cx"> }
</span></span></pre></div>
<a id="2013ahoerethtrunkincclasscoderevisionseditorsphp"></a>
<div class="addfile"><h4>Added: 2013/ahoereth/trunk/inc/class-code-revisions-editors.php (0 => 2186)</h4>
<pre class="diff"><span>
<span class="info">--- 2013/ahoereth/trunk/inc/class-code-revisions-editors.php (rev 0)
+++ 2013/ahoereth/trunk/inc/class-code-revisions-editors.php 2013-08-02 16:05:41 UTC (rev 2186)
</span><span class="lines">@@ -0,0 +1,376 @@
</span><ins>+<?php
+/**
+ * Class which contains functionality for the editor pages. Only required to be
+ * loaded there.
+ *
+ * @since 0.1
+ */
+class Code_Revisions_Editors {
+
+ /**
+ * ID of the post associated with the current file
+ *
+ * @var int
+ */
+ private $id;
+
+ /**
+ * Key of the post meta field containing the file's meta data
+ *
+ * @var string
+ */
+ private $metakey;
+
+ /**
+ * File meta data, also saved in post meta: "type", "package", "file"
+ *
+ * @var associative array
+ */
+ private $meta;
+
+ /**
+ * File title: "Package: subfolder/file.ext"
+ *
+ * @var string
+ */
+ private $title;
+
+ /**
+ * Initializes all actions needed on editor pages.
+ *
+ * @since 0.1
+ *
+ * @param string $metakey key used for the custom post meta containing file info
+ */
+ public function __construct() {
+ add_action( 'admin_init', array( &$this, 'init' ) );
+ add_action( 'admin_enqueue_scripts', array( &$this, 'scripts' ) );
+ add_action( 'admin_notices', array( &$this, 'syntax_notice' ) );
+
+ global $pagenow;
+ $this->meta['type'] = substr( $pagenow, 0, strpos( $pagenow, '-' ) );
+ $this->metakey = '_' . str_replace( '-', '_', CODE_REVISIONS_NAME );
+ }
+
+ /**
+ * Initializes class variables and more, depending on request data.
+ *
+ * @since 0.1
+ */
+ public function init() {
+ // generate meta class variables (meta & title)
+ $this->generate_meta();
+
+ // get id of the post associated with the current file
+ $this->id = $this->retrieve();
+
+ // remove the old draft if we are not viewing it
+ if ( ! isset( $_GET['syntax_error'] ) )
+ delete_post_meta( $this->id, $this->metakey . '_draft' );
+
+ // file update process
+ if ( isset( $_POST['action'] ) && 'update' == $_POST['action'] )
+ $this->handle_file_update();
+ else
+ $this->handle_direct_changes();
+ }
+
+ /**
+ * Enqueue scripts required on theme-editor.php and plugin-editor.php
+ *
+ * @since 0.2
+ *
+ * @param string $pagenow current admin page
+ */
+ function scripts( $pagenow ) {
+ //$suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';
+ $suffix = '';
+
+ wp_register_style( 'code_revisions_editors', CODE_REVISIONS_URL . "css/editors$suffix.css", array(), CODE_REVISIONS_VER );
+ wp_enqueue_style( 'code_revisions_editors' );
+
+ wp_register_script( 'code_revisions_editors', CODE_REVISIONS_URL . "js/editors$suffix.js", array( 'jquery' ), CODE_REVISIONS_VER, true );
+ wp_enqueue_script( 'code_revisions_editors' );
+
+ wp_localize_script( 'code_revisions_editors', '_code_revisions', array(
+ 'post_id' => $this->id,
+ 'revisions_list' => $this->get_revisions_list( $this->id ),
+ 'line' => isset( $_GET['syntax_error'] ) ? urldecode( $_GET['syntax_error'] ) : '',
+ 'newcontent' => get_post_meta( $this->id, $this->metakey . '_draft', true ),
+ ));
+ }
+
+ /**
+ * Process the information we can obtain from the GET or POST request;
+ * if there is no data generate default data.
+ *
+ * @since 0.1
+ */
+ private function generate_meta() {
+ // Use _GET and _POST data
+ $file = $f = ! empty( $_REQUEST['file' ] ) ? $_REQUEST['file' ] : '' ;
+ $package = $p = ! empty( $_REQUEST['theme' ] ) ? $_REQUEST['theme' ] :
+ ( ! empty( $_REQUEST['plugin'] ) ? $_REQUEST['plugin'] : '' );
+
+ // theme specific
+ if ( 'theme' == $this->meta['type'] ) {
+ // Get default values if necessary
+ $package = $p = ! empty( $package ) ? $package : get_stylesheet();
+ $file = $f = ! empty( $file ) ? $file : 'style.css';
+ }
+
+ // plugin specific
+ else {
+ // Passed $package often contains wrong values or is empty, fix it
+ if ( empty( $package ) || strpos($package, '/') ) {
+ $path = explode( '/', $package );
+ $data = get_plugins( '/' . $path[0] );
+ $plugins = array_keys( $data );
+ $package = ltrim( $path[0] . '/' . $plugins[0], '/' );
+
+ // plugins without subfolder
+ } else {
+ $data = get_plugin_data( WP_PLUGIN_DIR . '/' . $package );
+ }
+
+ // file is either passed or equal to $package
+ $file = empty( $file ) ? $package : $file;
+
+ // get clean plugin and file name
+ $path = explode( '/', $file );
+ array_shift( $path ); // get rid of plugin folder
+ $f = ! empty( $path[0] ) ? implode( '/', $path ) : $file;
+ $p = ! empty( $data['Name'] ) ? $data['Name'] : $data[ $plugins[0] ]['Name'];
+ }
+
+ // save meta array
+ $this->meta['package' ] = $package;
+ $this->meta['file' ] = $file;
+ $this->meta['checksum'] = md5_file( $this->get_abs() );
+
+ // build post title
+ $this->title = "$p: $f";
+ }
+
+ /**
+ * Gets the file's absolute path.
+ *
+ * @since 0.4
+ *
+ * @return string absolute file path
+ */
+ private function get_abs() {
+ extract( $this->meta );
+ return 'theme' == $type ? get_theme_root() . "/$package/$file" : WP_PLUGIN_DIR . "/$file";
+ }
+
+ /**
+ * If the file contents have actually changed this function inserts a post
+ * (if there is none for this file yet) and updates it.
+ *
+ * @since 0.1
+ *
+ * @return boolean success/failure; might not return but redirect
+ */
+ private function handle_file_update() {
+ $old = @file_get_contents( $this->get_abs() );
+
+ // Did we successfully get the file?
+ if ( false === $old )
+ return false;
+
+ // get new file content
+ $new = wp_unslash( $_POST['newcontent'] );
+
+ // did the file contents change?
+ if ( $old === $new )
+ return false;
+
+ // file wasn't saved to the database yet: save with old content
+ if ( ! $this->id )
+ $this->id = $this->dispatch( $old );
+
+ // check php files for parse errors
+ $check = 'php' == pathinfo( $this->get_abs(), PATHINFO_EXTENSION) ? $this->check_syntax( $new ) : true;
+
+ // update database entry with new content if there are no errors
+ if ( empty( $check['message'] ) ) {
+ return 0 < $this->dispatch( $new );
+
+ // syntax error found, save draft and redirect with error message
+ } else {
+ // save draft
+ update_post_meta( $this->id, $this->metakey . '_draft', $new );
+
+ // error info
+ $line = ! empty( $check['line' ] ) ? $check['line' ] : 0;
+ $message = ! empty( $check['message'] ) ? $check['message'] : 'Your changes contain errors, not saved.';
+
+ // redirect with error information and autosave id
+ wp_redirect( admin_url( $this->meta['type'].'-editor.php?file=' . urlencode( $this->meta['file'] ) . '&'.$this->meta['type'].'=' . urlencode( $this->meta['package'] ) . '&syntax_error=' . urlencode( $line ) . '&error_message=' . urlencode( $message ) ) );
+ exit;
+ }
+ }
+
+ /**
+ * Checks if the file changed since last visit and, if thats the case,
+ * notifies the user and updates the post.
+ *
+ * @since 0.4
+ */
+ private function handle_direct_changes() {
+ $meta = get_post_meta( $this->id, $this->metakey, true );
+
+ if ( empty( $meta['checksum'] ) || $meta['checksum'] == $this->meta['checksum'] )
+ return;
+
+ // add admin notice
+ add_action( 'admin_notices', array( &$this, 'notice' ) );
+
+ // read file
+ $new = @file_get_contents( $this->get_abs() );
+
+ // error occured when reading the file
+ if ( false === $new )
+ return false;
+
+ // update post
+ $this->dispatch( $new );
+ }
+
+ /**
+ * Used for displaying an admin notice above the editor which notifies the user
+ * that the file changed since his last visit.
+ *
+ * @since 0.4
+ * @uses wp_get_post_revisions()
+ * @uses get_edit_post_link()
+ */
+ public function notice() {
+ $revision = array_keys( wp_get_post_revisions( $this->id, array( 'numberposts' => 1 ) ) );
+ $link = get_edit_post_link( $revision[0] );
+ echo '<div class="updated"><p>' . __('This file has changed in the meantime.', 'code-revisions') . " <a href='$link'>" . __('View changes', 'code-revisions') . '</a></p></div>';
+ }
+
+ /**
+ * Used for displaying the admin notice when the file changes contained
+ *
+ * @since 0.5
+ */
+ public function syntax_notice() {
+ if ( ! isset( $_GET['syntax_error'] ) || empty( $_GET['error_message'] ) || ! is_numeric( $_GET['syntax_error'] ) )
+ return;
+
+ $line = urldecode( $_GET['syntax_error'] );
+ $message = ucfirst( urldecode( stripslashes( $_GET['error_message'] ) ) );
+
+ if ( $line > 0 )
+ echo '<div class="error"><p>File not saved, error in <a href="#" id="highlight_line">line ' . $line . '</a>: ' . $message . '</p></div>';
+ else
+ echo '<div class="error"><p>File not saved, an error occured: ' . $message . '</p></div>';
+ }
+
+ /**
+ * Basic php syntax checking.
+ *
+ * @since 0.5
+ *
+ * @param string $code php code to check for syntax errors
+ * @return array/boolean array contains type, message and line of error, if no error found returns true
+ */
+ function check_syntax( $code ) {
+ ob_start();
+ /* code needs to start with <?php itself but its not sure if it also ends with ?> */
+ var_dump(eval("return true; if(0){?>{$code}?><?php };"));
+ $error = error_get_last(); // type, message, line
+ ob_end_clean();
+
+ return ( ! empty( $error['message'] ) ) ? $error : true;
+ }
+
+ /**
+ * Depending on if a post for this file already exists in the database the
+ * function either creates a new post or updates the existing post with the
+ * new content.
+ *
+ * @since 0.4
+ * @uses wp_update_post()
+ * @uses wp_insert_post()
+ * @uses wp_save_post_revision()
+ *
+ * @param string $content new file contents
+ * @return int post id on success, otherwise zero
+ */
+ private function dispatch( $content ) {
+ $args = array(
+ 'post_type' => CODE_REVISIONS_POST_TYPE,
+ 'post_title' => $this->title,
+ 'post_name' => sanitize_title( $this->title ),
+ 'post_content' => $content,
+ 'post_status' => 'private',
+ );
+
+ // insert post and add first revision
+ if ( ! $this->id ){
+ $id = wp_insert_post( $args );
+ wp_save_post_revision( $id );
+ }
+
+ // update post
+ else {
+ $args['ID'] = $this->id;
+ $id = wp_update_post($args);
+ delete_post_meta( $this->id, $this->metakey . '_draft' );
+ }
+
+ // update/insert post meta with new checksum
+ $this->meta['checksum'] = md5( $content );
+ update_post_meta( $id, $this->metakey, $this->meta );
+
+ return $id;
+ }
+
+ /**
+ * Retrieves the ID of the post which corresponds to the current file
+ * (specified by $this->title) from the database.
+ *
+ * @since 0.1
+ * @uses get_posts()
+ *
+ * @return int post_id on success : zero on error
+ */
+ public function retrieve() {
+ $args = array(
+ 'name' => sanitize_title( $this->title ),
+ 'post_type' => CODE_REVISIONS_POST_TYPE,
+ 'post_status' => 'private',
+ 'numberposts' => 1,
+ );
+ $post = get_posts( $args );
+ return isset( $post[0] ) ? $post[0]->ID : 0;
+ }
+
+ /**
+ * wp_list_post_revisions() directly echos it's results. We want to work
+ * with it in JS and therefore need to buffer it. metabox.php furthermore adds
+ * the layout around the revisions list so we get a metabox similar to the
+ * originals on the post and page edit screens.
+ *
+ * NOTE: metabox.php requires $id to be set.
+ *
+ * @since 0.2
+ * @uses code-revisions/inc/metabox.php
+ * @uses wp_list_post_revisions()
+ *
+ * @param int $id The id of the post to get the revisions list for
+ * @return string Revisions list ready for printing.
+ */
+ function get_revisions_list( $id ) {
+ if ( ! $id )
+ return;
+
+ ob_start();
+ include_once( CODE_REVISIONS_DIR . 'inc/metabox.php' );
+ return ob_get_clean();
+ }
+}
</ins></span></pre></div>
<a id="2013ahoerethtrunkincclasscoderevisionsphp"></a>
<div class="addfile"><h4>Added: 2013/ahoereth/trunk/inc/class-code-revisions.php (0 => 2186)</h4>
<pre class="diff"><span>
<span class="info">--- 2013/ahoereth/trunk/inc/class-code-revisions.php (rev 0)
+++ 2013/ahoereth/trunk/inc/class-code-revisions.php 2013-08-02 16:05:41 UTC (rev 2186)
</span><span class="lines">@@ -0,0 +1,172 @@
</span><ins>+<?php
+/**
+ * Class required to be loaded on all WordPress pages. Initializes more
+ * functionality for specific pages, fixes redirects and handles restores.
+ *
+ * @since 0.1
+ */
+class Code_Revisions {
+
+ /**
+ * Key of the post meta field containing the file's meta data
+ *
+ * @var string
+ */
+ private $metakey;
+
+ /**
+ * Class constructor for adding actions to hook into core.
+ *
+ * @since 0.1
+ */
+ public function __construct() {
+ add_action( 'init', array( &$this, 'post_type' ) );
+ add_action( 'load-post.php', array( &$this, 'redirect' ) );
+ add_action( 'wp_restore_post_revision', array( &$this, 'restore' ), 10, 2 );
+ add_action( 'admin_enqueue_scripts', array( &$this, 'scripts' ) );
+
+ $this->metakey = '_' . str_replace( '-', '_', CODE_REVISIONS_NAME );
+ }
+
+ /**
+ * Enqueue scripts and styles.
+ *
+ * @since 0.4
+ */
+ public function scripts( $hook ) {
+ //$suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';
+ $suffix = '';
+
+ // revisions viewer wp-admin/revisions.php
+ if ( 'revision.php' != $hook )
+ return;
+
+ $rev_id = ! empty( $_GET['revision'] ) ? $_GET['revision'] : $_GET['from'];
+ $revision = get_post( $rev_id );
+ $post = get_post( $revision->post_parent );
+ if ( CODE_REVISIONS_POST_TYPE != $post->post_type )
+ return;
+
+ wp_register_style( 'code_revisions_viewer', CODE_REVISIONS_URL . "css/viewer.css", array(), CODE_REVISIONS_VER );
+ wp_enqueue_style( 'code_revisions_viewer' );
+ }
+
+ /**
+ * Register the custom post type for saving file contents
+ * as posts for revision capabilities.
+ *
+ * @since 0.1
+ * @uses register_post_type
+ */
+ public function post_type() {
+ register_post_type( CODE_REVISIONS_POST_TYPE, array(
+ 'labels' => array(
+ 'name' => __( 'Editor Files', 'code-revisions' ),
+ 'singular_name' => __( 'Editor File', 'code-revisions' ),
+ ),
+ 'public' => false,
+ 'rewrite' => false,
+ 'query_var' => false,
+ 'can_export' => false, // true?
+ // 'capabilities' => ?, // specification required?
+ 'supports' => array(
+ 'title',
+ 'editor',
+ 'author',
+ 'revisions'
+ ),
+ ));
+ }
+
+ /**
+ * Update file contents on revision restore.
+ *
+ * TODO: Possibly use checksums for enhanced security?
+ *
+ * @since 0.3
+ * @see wp_restore_post_revision(), wp-includes/revision.php:328
+ */
+ public function restore( $post_id, $revision_id ) {
+ // get post data
+ $post = get_post( $post_id );
+
+ // check post type
+ if ( $post->post_type != CODE_REVISIONS_POST_TYPE )
+ return;
+
+ // Its a file! Get revision data
+ $revision = get_post( $revision_id );
+
+ $meta = get_post_meta( $post->ID, $this->metakey, true );
+ extract( $meta ); // type, package, file
+
+ // get absolute file path
+ if ( 'theme' == $type ) {
+ $t = wp_get_theme( $package );
+ $abs = $t->get_stylesheet_directory() . '/' . $file;
+ } else {
+ $abs = WP_PLUGIN_DIR . '/' . $file;
+ }
+
+ if ( ! is_writeable( $abs ) )
+ return;
+
+ // open, write and close file
+ $f = fopen( $abs, 'w+' );
+ fwrite( $f, $revision->post_content );
+ fclose( $f );
+
+ // update checksum in database
+ $meta['checksum'] = md5( $revision->post_content );
+ update_post_meta( $post->ID, $this->metakey, $meta );
+
+ // theme specific: reset cache
+ if ( 'theme' == $type ) {
+ $theme = wp_get_theme( $package );
+ $theme->cache_delete();
+ }
+
+ // plugin specific: deactivate and reactivate
+ else {
+ $network_wide = is_plugin_active_for_network( $package );
+
+ // deactivate plugin
+ if ( is_plugin_active( $package ) )
+ deactivate_plugins( $package, true );
+
+ if ( ! is_network_admin() )
+ update_option( 'recently_activated', array( $package => time() ) + (array) get_option( 'recently_activated' ) );
+
+ $error = validate_plugin( $package );
+ if ( is_wp_error( $error ) )
+ wp_die( $error );
+
+ // reactivate plugin
+ if ( ( ! empty( $networkwide ) && ! is_plugin_active_for_network( $package ) ) || ! is_plugin_active( $package ) )
+ activate_plugin($file, '', ! empty( $networkwide ) );
+ }
+ }
+
+ /**
+ * Redirect from post editor to file editor.
+ *
+ * @since 0.2
+ * @uses wp_redirect
+ */
+ public function redirect() {
+ if ( ! isset( $_REQUEST['post'] ) || ! is_numeric( $_REQUEST['post'] ) )
+ return;
+
+ // get post data
+ $post = get_post( $_REQUEST['post'] );
+
+ // check post type
+ if ( $post->post_type != CODE_REVISIONS_POST_TYPE )
+ return;
+
+ // It's a file, not a post! Redirect to corresponding code editor.
+ extract( get_post_meta( $post->ID, $this->metakey, true ) );
+ wp_redirect( admin_url( "$type-editor.php?$type=".urlencode($package)."&file=".urlencode($file) ) );
+ exit;
+ }
+}
</ins></span></pre></div>
<a id="2013ahoerethtrunkinceditorsphp"></a>
<div class="delfile"><h4>Deleted: 2013/ahoereth/trunk/inc/editors.php (2185 => 2186)</h4>
<pre class="diff"><span>
<span class="info">--- 2013/ahoereth/trunk/inc/editors.php 2013-08-01 23:53:09 UTC (rev 2185)
+++ 2013/ahoereth/trunk/inc/editors.php 2013-08-02 16:05:41 UTC (rev 2186)
</span><span class="lines">@@ -1,376 +0,0 @@
</span><del>-<?php
-/**
- * Class which contains functionality for the editor pages. Only required to be
- * loaded there.
- *
- * @since 0.1
- */
-class Code_Revisions_Editors {
-
- /**
- * ID of the post associated with the current file
- *
- * @var int
- */
- private $id;
-
- /**
- * Key of the post meta field containing the file's meta data
- *
- * @var string
- */
- private $metakey;
-
- /**
- * File meta data, also saved in post meta: "type", "package", "file"
- *
- * @var associative array
- */
- private $meta;
-
- /**
- * File title: "Package: subfolder/file.ext"
- *
- * @var string
- */
- private $title;
-
- /**
- * Initializes all actions needed on editor pages.
- *
- * @since 0.1
- *
- * @param string $metakey key used for the custom post meta containing file info
- */
- public function __construct() {
- add_action( 'admin_init', array( &$this, 'init' ) );
- add_action( 'admin_enqueue_scripts', array( &$this, 'scripts' ) );
- add_action( 'admin_notices', array( &$this, 'syntax_notice' ) );
-
- global $pagenow;
- $this->meta['type'] = substr( $pagenow, 0, strpos( $pagenow, '-' ) );
- $this->metakey = '_' . str_replace( '-', '_', CODE_REVISIONS_NAME );
- }
-
- /**
- * Initializes class variables and more, depending on request data.
- *
- * @since 0.1
- */
- public function init() {
- // generate meta class variables (meta & title)
- $this->generate_meta();
-
- // get id of the post associated with the current file
- $this->id = $this->retrieve();
-
- // remove the old draft if we are not viewing it
- if ( ! isset( $_GET['syntax_error'] ) )
- delete_post_meta( $this->id, $this->metakey . '_draft' );
-
- // file update process
- if ( isset( $_POST['action'] ) && 'update' == $_POST['action'] )
- $this->handle_file_update();
- else
- $this->handle_direct_changes();
- }
-
- /**
- * Enqueue scripts required on theme-editor.php and plugin-editor.php
- *
- * @since 0.2
- *
- * @param string $pagenow current admin page
- */
- function scripts( $pagenow ) {
- //$suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';
- $suffix = '';
-
- wp_register_style( 'code_revisions_editors', CODE_REVISIONS_URL . "css/editors$suffix.css", array(), CODE_REVISIONS_VER );
- wp_enqueue_style( 'code_revisions_editors' );
-
- wp_register_script( 'code_revisions_editors', CODE_REVISIONS_URL . "js/editors$suffix.js", array( 'jquery' ), CODE_REVISIONS_VER, true );
- wp_enqueue_script( 'code_revisions_editors' );
-
- wp_localize_script( 'code_revisions_editors', '_code_revisions', array(
- 'post_id' => $this->id,
- 'revisions_list' => $this->get_revisions_list( $this->id ),
- 'line' => isset( $_GET['syntax_error'] ) ? urldecode( $_GET['syntax_error'] ) : '',
- 'newcontent' => get_post_meta( $this->id, $this->metakey . '_draft', true ),
- ));
- }
-
- /**
- * Process the information we can obtain from the GET or POST request;
- * if there is no data generate default data.
- *
- * @since 0.1
- */
- private function generate_meta() {
- // Use _GET and _POST data
- $file = $f = ! empty( $_REQUEST['file' ] ) ? $_REQUEST['file' ] : '' ;
- $package = $p = ! empty( $_REQUEST['theme' ] ) ? $_REQUEST['theme' ] :
- ( ! empty( $_REQUEST['plugin'] ) ? $_REQUEST['plugin'] : '' );
-
- // theme specific
- if ( 'theme' == $this->meta['type'] ) {
- // Get default values if necessary
- $package = $p = ! empty( $package ) ? $package : get_stylesheet();
- $file = $f = ! empty( $file ) ? $file : 'style.css';
- }
-
- // plugin specific
- else {
- // Passed $package often contains wrong values or is empty, fix it
- if ( empty( $package ) || strpos($package, '/') ) {
- $path = explode( '/', $package );
- $data = get_plugins( '/' . $path[0] );
- $plugins = array_keys( $data );
- $package = ltrim( $path[0] . '/' . $plugins[0], '/' );
-
- // plugins without subfolder
- } else {
- $data = get_plugin_data( WP_PLUGIN_DIR . '/' . $package );
- }
-
- // file is either passed or equal to $package
- $file = empty( $file ) ? $package : $file;
-
- // get clean plugin and file name
- $path = explode( '/', $file );
- array_shift( $path ); // get rid of plugin folder
- $f = ! empty( $path[0] ) ? implode( '/', $path ) : $file;
- $p = ! empty( $data['Name'] ) ? $data['Name'] : $data[ $plugins[0] ]['Name'];
- }
-
- // save meta array
- $this->meta['package' ] = $package;
- $this->meta['file' ] = $file;
- $this->meta['checksum'] = md5_file( $this->get_abs() );
-
- // build post title
- $this->title = "$p: $f";
- }
-
- /**
- * Gets the file's absolute path.
- *
- * @since 0.4
- *
- * @return string absolute file path
- */
- private function get_abs() {
- extract( $this->meta );
- return 'theme' == $type ? get_theme_root() . "/$package/$file" : WP_PLUGIN_DIR . "/$file";
- }
-
- /**
- * If the file contents have actually changed this function inserts a post
- * (if there is none for this file yet) and updates it.
- *
- * @since 0.1
- *
- * @return boolean success/failure; might not return but redirect
- */
- private function handle_file_update() {
- $old = @file_get_contents( $this->get_abs() );
-
- // Did we successfully get the file?
- if ( false === $old )
- return false;
-
- // get new file content
- $new = wp_unslash( $_POST['newcontent'] );
-
- // did the file contents change?
- if ( $old === $new )
- return false;
-
- // file wasn't saved to the database yet: save with old content
- if ( ! $this->id )
- $this->id = $this->dispatch( $old );
-
- // check php files for parse errors
- $check = 'php' == pathinfo( $this->get_abs(), PATHINFO_EXTENSION) ? $this->check_syntax( $new ) : true;
-
- // update database entry with new content if there are no errors
- if ( empty( $check['message'] ) ) {
- return 0 < $this->dispatch( $new );
-
- // syntax error found, save draft and redirect with error message
- } else {
- // save draft
- update_post_meta( $this->id, $this->metakey . '_draft', $new );
-
- // error info
- $line = ! empty( $check['line' ] ) ? $check['line' ] : 0;
- $message = ! empty( $check['message'] ) ? $check['message'] : 'Your changes contain errors, not saved.';
-
- // redirect with error information and autosave id
- wp_redirect( admin_url( $this->meta['type'].'-editor.php?file=' . urlencode( $this->meta['file'] ) . '&'.$this->meta['type'].'=' . urlencode( $this->meta['package'] ) . '&syntax_error=' . urlencode( $line ) . '&error_message=' . urlencode( $message ) ) );
- exit;
- }
- }
-
- /**
- * Checks if the file changed since last visit and, if thats the case,
- * notifies the user and updates the post.
- *
- * @since 0.4
- */
- private function handle_direct_changes() {
- $meta = get_post_meta( $this->id, $this->metakey, true );
-
- if ( empty( $meta['checksum'] ) || $meta['checksum'] == $this->meta['checksum'] )
- return;
-
- // add admin notice
- add_action( 'admin_notices', array( &$this, 'notice' ) );
-
- // read file
- $new = @file_get_contents( $this->get_abs() );
-
- // error occured when reading the file
- if ( false === $new )
- return false;
-
- // update post
- $this->dispatch( $new );
- }
-
- /**
- * Used for displaying an admin notice above the editor which notifies the user
- * that the file changed since his last visit.
- *
- * @since 0.4
- * @uses wp_get_post_revisions()
- * @uses get_edit_post_link()
- */
- public function notice() {
- $revision = array_keys( wp_get_post_revisions( $this->id, array( 'numberposts' => 1 ) ) );
- $link = get_edit_post_link( $revision[0] );
- echo '<div class="updated"><p>' . __('This file has changed in the meantime.', 'code-revisions') . " <a href='$link'>" . __('View changes', 'code-revisions') . '</a></p></div>';
- }
-
- /**
- * Used for displaying the admin notice when the file changes contained
- *
- * @since 0.5
- */
- public function syntax_notice() {
- if ( ! isset( $_GET['syntax_error'] ) || empty( $_GET['error_message'] ) || ! is_numeric( $_GET['syntax_error'] ) )
- return;
-
- $line = urldecode( $_GET['syntax_error'] );
- $message = ucfirst( urldecode( stripslashes( $_GET['error_message'] ) ) );
-
- if ( $line > 0 )
- echo '<div class="error"><p>File not saved, error in <a href="#" id="highlight_line">line ' . $line . '</a>: ' . $message . '</p></div>';
- else
- echo '<div class="error"><p>File not saved, an error occured: ' . $message . '</p></div>';
- }
-
- /**
- * Basic php syntax checking.
- *
- * @since 0.5
- *
- * @param string $code php code to check for syntax errors
- * @return array/boolean array contains type, message and line of error, if no error found returns true
- */
- function check_syntax( $code ) {
- ob_start();
- /* code needs to start with <?php itself but its not sure if it also ends with ?> */
- var_dump(eval("return true; if(0){?>{$code}?><?php };"));
- $error = error_get_last(); // type, message, line
- ob_end_clean();
-
- return ( ! empty( $error['message'] ) ) ? $error : true;
- }
-
- /**
- * Depending on if a post for this file already exists in the database the
- * function either creates a new post or updates the existing post with the
- * new content.
- *
- * @since 0.4
- * @uses wp_update_post()
- * @uses wp_insert_post()
- * @uses wp_save_post_revision()
- *
- * @param string $content new file contents
- * @return int post id on success, otherwise zero
- */
- private function dispatch( $content ) {
- $args = array(
- 'post_type' => CODE_REVISIONS_POST_TYPE,
- 'post_title' => $this->title,
- 'post_name' => sanitize_title( $this->title ),
- 'post_content' => $content,
- 'post_status' => 'private',
- );
-
- // insert post and add first revision
- if ( ! $this->id ){
- $id = wp_insert_post( $args );
- wp_save_post_revision( $id );
- }
-
- // update post
- else {
- $args['ID'] = $this->id;
- $id = wp_update_post($args);
- delete_post_meta( $this->id, $this->metakey . '_draft' );
- }
-
- // update/insert post meta with new checksum
- $this->meta['checksum'] = md5( $content );
- update_post_meta( $id, $this->metakey, $this->meta );
-
- return $id;
- }
-
- /**
- * Retrieves the ID of the post which corresponds to the current file
- * (specified by $this->title) from the database.
- *
- * @since 0.1
- * @uses get_posts()
- *
- * @return int post_id on success : zero on error
- */
- public function retrieve() {
- $args = array(
- 'name' => sanitize_title( $this->title ),
- 'post_type' => CODE_REVISIONS_POST_TYPE,
- 'post_status' => 'private',
- 'numberposts' => 1,
- );
- $post = get_posts( $args );
- return isset( $post[0] ) ? $post[0]->ID : 0;
- }
-
- /**
- * wp_list_post_revisions() directly echos it's results. We want to work
- * with it in JS and therefore need to buffer it. metabox.php furthermore adds
- * the layout around the revisions list so we get a metabox similar to the
- * originals on the post and page edit screens.
- *
- * NOTE: metabox.php requires $id to be set.
- *
- * @since 0.2
- * @uses code-revisions/inc/metabox.php
- * @uses wp_list_post_revisions()
- *
- * @param int $id The id of the post to get the revisions list for
- * @return string Revisions list ready for printing.
- */
- function get_revisions_list( $id ) {
- if ( ! $id )
- return;
-
- ob_start();
- include_once( CODE_REVISIONS_DIR . 'inc/metabox.php' );
- return ob_get_clean();
- }
-}
</del></span></pre></div>
<a id="2013ahoerethtrunkincmainphp"></a>
<div class="delfile"><h4>Deleted: 2013/ahoereth/trunk/inc/main.php (2185 => 2186)</h4>
<pre class="diff"><span>
<span class="info">--- 2013/ahoereth/trunk/inc/main.php 2013-08-01 23:53:09 UTC (rev 2185)
+++ 2013/ahoereth/trunk/inc/main.php 2013-08-02 16:05:41 UTC (rev 2186)
</span><span class="lines">@@ -1,172 +0,0 @@
</span><del>-<?php
-/**
- * Class required to be loaded on all WordPress pages. Initializes more
- * functionality for specific pages, fixes redirects and handles restores.
- *
- * @since 0.1
- */
-class Code_Revisions {
-
- /**
- * Key of the post meta field containing the file's meta data
- *
- * @var string
- */
- private $metakey;
-
- /**
- * Class constructor for adding actions to hook into core.
- *
- * @since 0.1
- */
- public function __construct() {
- add_action( 'init', array( &$this, 'post_type' ) );
- add_action( 'load-post.php', array( &$this, 'redirect' ) );
- add_action( 'wp_restore_post_revision', array( &$this, 'restore' ), 10, 2 );
- add_action( 'admin_enqueue_scripts', array( &$this, 'scripts' ) );
-
- $this->metakey = '_' . str_replace( '-', '_', CODE_REVISIONS_NAME );
- }
-
- /**
- * Enqueue scripts and styles.
- *
- * @since 0.4
- */
- public function scripts( $hook ) {
- //$suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';
- $suffix = '';
-
- // revisions viewer wp-admin/revisions.php
- if ( 'revision.php' != $hook )
- return;
-
- $rev_id = ! empty( $_GET['revision'] ) ? $_GET['revision'] : $_GET['from'];
- $revision = get_post( $rev_id );
- $post = get_post( $revision->post_parent );
- if ( CODE_REVISIONS_POST_TYPE != $post->post_type )
- return;
-
- wp_register_style( 'code_revisions_viewer', CODE_REVISIONS_URL . "css/viewer.css", array(), CODE_REVISIONS_VER );
- wp_enqueue_style( 'code_revisions_viewer' );
- }
-
- /**
- * Register the custom post type for saving file contents
- * as posts for revision capabilities.
- *
- * @since 0.1
- * @uses register_post_type
- */
- public function post_type() {
- register_post_type( CODE_REVISIONS_POST_TYPE, array(
- 'labels' => array(
- 'name' => __( 'Editor Files', 'code-revisions' ),
- 'singular_name' => __( 'Editor File', 'code-revisions' ),
- ),
- 'public' => false,
- 'rewrite' => false,
- 'query_var' => false,
- 'can_export' => false, // true?
- // 'capabilities' => ?, // specification required?
- 'supports' => array(
- 'title',
- 'editor',
- 'author',
- 'revisions'
- ),
- ));
- }
-
- /**
- * Update file contents on revision restore.
- *
- * TODO: Possibly use checksums for enhanced security?
- *
- * @since 0.3
- * @see wp_restore_post_revision(), wp-includes/revision.php:328
- */
- public function restore( $post_id, $revision_id ) {
- // get post data
- $post = get_post( $post_id );
-
- // check post type
- if ( $post->post_type != CODE_REVISIONS_POST_TYPE )
- return;
-
- // Its a file! Get revision data
- $revision = get_post( $revision_id );
-
- $meta = get_post_meta( $post->ID, $this->metakey, true );
- extract( $meta ); // type, package, file
-
- // get absolute file path
- if ( 'theme' == $type ) {
- $t = wp_get_theme( $package );
- $abs = $t->get_stylesheet_directory() . '/' . $file;
- } else {
- $abs = WP_PLUGIN_DIR . '/' . $file;
- }
-
- if ( ! is_writeable( $abs ) )
- return;
-
- // open, write and close file
- $f = fopen( $abs, 'w+' );
- fwrite( $f, $revision->post_content );
- fclose( $f );
-
- // update checksum in database
- $meta['checksum'] = md5( $revision->post_content );
- update_post_meta( $post->ID, $this->metakey, $meta );
-
- // theme specific: reset cache
- if ( 'theme' == $type ) {
- $theme = wp_get_theme( $package );
- $theme->cache_delete();
- }
-
- // plugin specific: deactivate and reactivate
- else {
- $network_wide = is_plugin_active_for_network( $package );
-
- // deactivate plugin
- if ( is_plugin_active( $package ) )
- deactivate_plugins( $package, true );
-
- if ( ! is_network_admin() )
- update_option( 'recently_activated', array( $package => time() ) + (array) get_option( 'recently_activated' ) );
-
- $error = validate_plugin( $package );
- if ( is_wp_error( $error ) )
- wp_die( $error );
-
- // reactivate plugin
- if ( ( ! empty( $networkwide ) && ! is_plugin_active_for_network( $package ) ) || ! is_plugin_active( $package ) )
- activate_plugin($file, '', ! empty( $networkwide ) );
- }
- }
-
- /**
- * Redirect from post editor to file editor.
- *
- * @since 0.2
- * @uses wp_redirect
- */
- public function redirect() {
- if ( ! isset( $_REQUEST['post'] ) || ! is_numeric( $_REQUEST['post'] ) )
- return;
-
- // get post data
- $post = get_post( $_REQUEST['post'] );
-
- // check post type
- if ( $post->post_type != CODE_REVISIONS_POST_TYPE )
- return;
-
- // It's a file, not a post! Redirect to corresponding code editor.
- extract( get_post_meta( $post->ID, $this->metakey, true ) );
- wp_redirect( admin_url( "$type-editor.php?$type=".urlencode($package)."&file=".urlencode($file) ) );
- exit;
- }
-}
</del></span></pre>
</div>
</div>
</body>
</html>