<!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>[2116] 2013/ahoereth/trunk: Move Code_Revisions class to individual file.</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/2116">2116</a></dd>
<dt>Author</dt> <dd>a.hoereth</dd>
<dt>Date</dt> <dd>2013-07-15 15:04:49 +0000 (Mon, 15 Jul 2013)</dd>
</dl>

<h3>Log Message</h3>
<pre>Move Code_Revisions class to individual file. 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="#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 (2115 => 2116)</h4>
<pre class="diff"><span>
<span class="info">--- 2013/ahoereth/trunk/code-revisions.php   2013-07-14 17:11:32 UTC (rev 2115)
+++ 2013/ahoereth/trunk/code-revisions.php      2013-07-15 15:04:49 UTC (rev 2116)
</span><span class="lines">@@ -25,7 +25,7 @@
</span><span class="cx"> */
</span><span class="cx"> 
</span><span class="cx"> if (!defined('CODE_REVISIONS_VER'))
</span><del>-       define('CODE_REVISIONS_VER', '0.2');
</del><ins>+        define('CODE_REVISIONS_VER', '0.3');
</ins><span class="cx"> if ( ! defined('CODE_REVISIONS_NAME') )
</span><span class="cx">  define( 'CODE_REVISIONS_NAME', basename(__FILE__, '.php') );
</span><span class="cx"> if ( ! defined('CODE_REVISIONS_DIR' ) )
</span><span class="lines">@@ -35,190 +35,22 @@
</span><span class="cx"> if ( !defined('CODE_REVISIONS_POST_TYPE') )
</span><span class="cx">  define('CODE_REVISIONS_POST_TYPE', 'code');
</span><span class="cx"> 
</span><del>-/**
- * Class required to be loaded on all WordPress pages. Initializes more
- * functionality for specific pages, fixes redirects and handles restores.
</del><ins>+
+/*
+ * Initialize the main class - required on all pages. It registers the custom
+ * post type etc.
+ */
+include_once( CODE_REVISIONS_DIR . 'inc/main.php' );
+new Code_Revisions;
+
+/*
+ * Initialize the Code_Revisions_Editors class only when required: On
+ * theme-editor.php and plugin-editor.php
</ins><span class="cx">  *
</span><del>- * @since 0.1
</del><ins>+ * @uses $pagenow
</ins><span class="cx">  */
</span><del>-class Code_Revisions {
-
-       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( 'init',                     array( &$this, 'editors'   )        );
-               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'
-                       ),
-               ));
-       }
-
-       /**
-        * Function for creating the editor page specific class when required. Fired
-        * on admin_init. Cannot use load-theme-editor.php and load-plugin-editor.php
-        * hooks here because the editors class needs to access the admin_init hook
-        * itself.
-        *
-        * @since 0.1
-        */
-       public function editors() {
-               global $pagenow;
-
-               if ( in_array( $pagenow, array( 'theme-editor.php', 'plugin-editor.php' ) ) ) {
-                       $type = substr( $pagenow, 0, strpos( $pagenow, '-' ) );
-
-                       include_once( CODE_REVISIONS_DIR . 'inc/editors.php' );
-                       new Code_Revisions_Editors( $this->metakey, $type );
-               }
-       }
-
-       /**
-        * 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><ins>+global $pagenow;
+if ( in_array( $pagenow, array( 'theme-editor.php', 'plugin-editor.php' ) ) ) {
+       include_once( CODE_REVISIONS_DIR . 'inc/editors.php' );
+       new Code_Revisions_Editors;
</ins><span class="cx"> }
</span><del>-new Code_Revisions;
</del></span></pre></div>
<a id="2013ahoerethtrunkincmainphp"></a>
<div class="addfile"><h4>Added: 2013/ahoereth/trunk/inc/main.php (0 => 2116)</h4>
<pre class="diff"><span>
<span class="info">--- 2013/ahoereth/trunk/inc/main.php                         (rev 0)
+++ 2013/ahoereth/trunk/inc/main.php    2013-07-15 15:04:49 UTC (rev 2116)
</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>
</div>

</body>
</html>