WordPress.org

Ready to get started?Download WordPress

Forums

Template Sync
updated plugin file attached with instructions (1 post)

  1. Iamhere
    Member
    Posted 2 years ago #

    Please find below the updated file that now incorporates HTTPS conversion and also includes simplified instructions.

    All credit to the original author.
    To use: just copy / paste this over your installed templatesync.php file in the plugin

    <?php
    
    /*
    
    Plugin Name: Template Sync
    
    Plugin URI: http://wordpress.org/extend/plugins/templatesync/
    
    Description: Copy the HTML from your WordPress theme to another application's template library.
    
    Author: Shannon Whitley
    
    Version: 1.00
    
    Author URI: http://voiceoftech.com/swhitley/
    
    */
    
    function tsync_template_update()
    
    {
    
    	$tsync_include_dir = get_option('tsync_include_dir');
    
    	$tsync_header_file = get_option('tsync_header_file');
    
    	$tsync_footer_file = get_option('tsync_footer_file');
    
    	$tsync_smarty = get_option('tsync_smarty');
    
    	$tsync_https = get_option('tsync_https');
    	$tsync_httpsremove = get_option('tsync_httpsremove');
    
    	//Retrieve the home page
    
    	$response = wp_remote_get(get_bloginfo( 'url' ));
    
    	if( !is_wp_error( $response ) )
    
    	{
    
    		//Remove Excluded Code
    
    		$html = $response[body];
    		$headerTop = $response[header]; // strip out the top of the file e.g. doc type etc...
    
    		//Exclude sections.
    
    		// original borked
    		// $html = preg_replace("/<!--tpl.exclude-->(.*?)<!--\/tpl.exclude-->/", '', $html);
    
    		   $html = preg_replace("/<!--tpl.exclude-->(.*?)<!--\/tpl.exclude-->/eis", '', $html);
    		   $headerTop = preg_replace("/<!--tpl.exclude-->(.*?)<!--\/tpl.exclude-->/eis", '', $headerTop);
    
    		//Include files.
    
    		preg_match_all("/<!--(.*?)\.inc-->/", $html, $tsyncinclude);
    
    		if(count($tsyncinclude) > 1)
    
    		{
    
    			$tsyncinclude = $tsyncinclude[1];
    
    			foreach($tsyncinclude as $include)
    
    			{
    
    				$includeFile = $tsync_include_dir.$include.".inc";
    
    				if(file_exists($includeFile))
    
    				{
    
    					$fh = fopen($includeFile, 'r') or wp_die("Can't open ".$includeFile.".  Please make sure read permissions are enabled.");
    
    					$data = fread($fh, filesize($includeFile)); 
    
    					fclose($fh);
    
    					$html = str_replace('<!--'.$include.'.inc-->', $data, $html);
    
    				}
    
    			}
    
    		}
    
    		//Write the Header
    
    		preg_match("/^(.*?)<!--header.end-->/s", $html, $header);
    
    		if(count($header) > 0)
    
    		{
    
    			$headerFile = $tsync_header_file;
    
    			$fh = fopen($headerFile, 'w') or wp_die("Can't open ".$headerFile.".  Please make sure write permissions are enabled.");
    
       		    $header = $header[0];
    
    			if($tsync_https !== 'N')
    
    			{
    
    				$header = tsync_replace_http($header);
    
    			}
    
    			{
    
    				$header = tsync_replace_httpremove($header);
    
    			}
    
    			if($tsync_smarty !== 'N')
    
    			{
    
    				$header = tsync_smarty_literal($header);
    
    			}
    
    			fwrite($fh, $header);
    
    			fclose($fh);
    
    		}
    
    		//Write the Footer
    
    		preg_match("/<!--footer.begin-->(.*?)<\/html>/s", $html, $footer);
    
    		if(count($footer) > 0)
    
    		{
    
    			$footerFile = $tsync_footer_file;
    
    			$fh = fopen($footerFile, 'w') or wp_die("Can't open ".footerFile.".  Please make sure write permissions are enabled.");
    
    			$footer = $footer[0];
    
    			if($tsync_https !== 'N')
    
    			{
    
    				$footer = tsync_replace_http($footer);
    
    			}
    
    			{
    
    				$header = tsync_replace_httpremove($header);
    
    			}
    
    			if($tsync_smarty !== 'N')
    
    			{
    
    				$footer = tsync_smarty_literal($footer);
    
    			}
    
    			fwrite($fh, $footer);
    
    			fclose($fh);
    
    		}
    
    	}
    
    }
    
    //Turn script and style tags into smarty tag literals.
    
    function tsync_smarty_literal($html)
    
    {
    
    	$html = str_replace('</script>','</script>{/literal}',str_replace('<script','{literal}<script',$html));
    
    	$html = str_replace('</style>','</style>{/literal}',str_replace('<style','{literal}<style',$html));
    
    	return $html;
    
    }
    
    //function to convert http links to https:
    
    function tsync_replace_http( $https ) {
        $https = str_replace('http://', 'https://', $https );
    
        return $https;
    }
    
    //Reverse function to convert https links to http:
    
    function tsync_replace_httpremove( $httpsremove ) {
       	$httpsremove = str_replace('https://', 'http://', $httpsremove );
    
        return $httpsremove;
    }
    
    add_action('admin_menu', 'tsync_menu');
    
    function tsync_menu() {
    
      add_options_page('Template Sync Settings', 'Template Sync', 'manage_options', 'tsync_settings_menu', 'tsync_plugin_options');
    
    }
    
    //* S E T T I N G S
    
    function tsync_plugin_options() {
    
    	if (!current_user_can('manage_options'))  {
    
    		wp_die( __('You do not have sufficient permissions to access this page.') );
    
    	}
    
    	$tsync_include_dir = get_option('tsync_include_dir');
    
    	$tsync_header_file = get_option('tsync_header_file');
    
    	$tsync_footer_file = get_option('tsync_footer_file');
    
    	$tsync_smarty = get_option('tsync_smarty');
    
    	if(empty($tsync_smarty))
    
    	{
    
    		$tsync_smarty = 'Y';
    
    	}
    
    	$tsync_https = get_option('tsync_https');
    
    	if(empty($tsync_https))
    
    	{
    
    		$tsync_https = 'Y';
    
    	}
    
    	$tsync_httpsremove = get_option('tsync_httpsremove');
    
    	if(empty($tsync_httpsremove))
    
    	{
    
    		$tsync_httpsremove = 'N';
    
    	}
    
    	if(isset($_POST['tsyncbtnUpdate']))
    
    	{
    
    		$tsync_include_dir = $_POST['tsync_include_dir'];
    
    		$tsync_header_file = $_POST['tsync_header_file'];
    
    		$tsync_footer_file = $_POST['tsync_footer_file'];
    
    		$tsync_smarty = $_POST['tsync_smarty'] == 'Y' ? 'Y' : 'N';
    
    		$tsync_https = $_POST['tsync_https'] == 'Y' ? 'Y' : 'N';
    
    		update_option('tsync_include_dir', $tsync_include_dir);
    
    		update_option('tsync_header_file', $tsync_header_file);
    
    		update_option('tsync_footer_file', $tsync_footer_file);
    
    		update_option('tsync_smarty', $tsync_smarty);
    
    		update_option('tsync_https', $tsync_https);
    
    		update_option('tsync_httpsremove', $tsync_httpsremove);
    
    		tsync_template_update();
    
    	}
    
    	$tsync_smarty = $tsync_smarty == 'Y' ? 'checked="checked"' : '';
    	$tsync_https = $tsync_https == 'Y' ? 'checked="checked"' : '';
    	$tsync_httpsremove = $tsync_httpsremove == 'Y' ? 'checked="unchecked' : '';
    
    ?>
    
    <div class="wrap">
    
    <h1>Template Sync Settings</h1>
    
    <?php if(isset($_POST['tsyncbtnUpdate'])):?>
    
         <div id="message" class="updated fade"><p><?php _e('Template edited successfully.') ?></p></div>
    
    <?php endif; ?>
    
    <form method="post">
    
    <p>This plugin will duplicate the content of your WordPress Template file(s).  It writes the source code (similar to that seen when you "view source") to your chosen file location. To use it, you will need to manually insert the following code snippets into the WordPress theme files that you want to sync. </p>
    <p>The snippets will be used to tell the sync plugin where to start/stop copying and what to include when writing your WP template to your recipient application's template files.</p>
    
    <p>This is a duplication tool - it does not enable dynamic functionality with your target application. In other words, whenever you change your WordPress template files (e.g. header / footer), you will need to come back here and manually perform another <strong>Sync Template</strong> action if you want the updates to reflect onto your recipient application.
    
    </p>
    <h3><strong>Explanations:</strong></h3>
    
    <p><strong>Start / Stop snippets</strong></p>
    <table cellpadding="5">
    
      <tr>
    
    <td><!--header.end--></td><td>Add this to your WordPress template code where you want the Sync to stop (e.g. at the end of your WordPress header.php template file).</td></tr>
    
    <tr>
    
    <td><!--footer.begin--></td><td>Add this at the beginning of your WordPress footer template code where you want the Sync to stop footer code (e.g. in your WordPress footer.php).</td></tr>
    
    </table>
    
    </p>
    
    <p><strong>Exclude snippets </strong></p>
    <table cellpadding="5">
      <tr>
        <td><!--tpl.exclude--></td>
        <td>Add this to your WordPress template code where you want the Sync to begin excluding your text</td>
      </tr>
      <tr>
        <td><!--/tpl.exclude--></td>
        <td>Add this to your WordPress template code where you want the Sync to end excluding your text</td>
      </tr>
    </table>
    <p><strong>Include file contents from another template directory.</strong></p>
    <table cellpadding="5">
    
      <tr>
    
    <td><strong><!--{unique id}.inc--></strong></td><td>The contents of <?php echo $tsync_include_dir; ?>{unique id}.inc will be added to the code at the specified location.</td></tr>
    
    </table>
    
    </p>
    
    <p><strong>Write to  file / path: </strong></p>
    <p>Add the full server path to the file you want to write to. If the file does not exist, it will be created. For example: /home/wwwexample/public_html/clients/templates/default/wp-header.tpl </p>
    
    <p>
      </p></p>
    <p><strong>Smarty code</strong> option is primarily for users wishing to transfer their WordPress template to a WHMCS installation (if you don't know what this is just <strong>untick</strong> it).</p>
    <p><strong>HTTP / HTTPS:</strong> If your recipient application does or does not use SSL, you may wish to convert links by choosing the option below.</p>
    
    <hr />
    
    <h3>Settings </h3>
    <table cellpadding="5">
    
    <tr><td>Include files from this directory:</td><td><input id="tsync_include_dir" name="tsync_include_dir" type="text" value="<?php echo $tsync_include_dir; ?>" size="100" /></td></tr>
    
    <tr>
      <td>Write the header to this file (include path):</td><td><input id="tsync_header_file" name="tsync_header_file" type="text" value="<?php echo $tsync_header_file; ?>" size="100"/></td></tr>
    
    <tr><td>Write the footer to this file (include path):</td><td><input id="tsync_footer_file" name="tsync_footer_file" type="text" value="<?php echo $tsync_footer_file; ?>" size="100"/></td></tr>
    
    <tr><td>Use <a href="http://www.smarty.net/" target="_blank">Smarty</a> template literals?</td><td><input id="tsync_smarty" name="tsync_smarty" type="checkbox" value="Y" <?php echo $tsync_smarty; ?> /></td></tr>
    
    <tr><td>Convert all http:// links to https:// <br />
      (select this if you're using SSL on the receiving end) ?</td><td>
      <input id="tsync_https" name="tsync_https" type="checkbox" value="Y" <?php echo $tsync_https; ?> /></td></tr>
    
    <tr>
      <td>Reverse convert all https:// links to http:// <br />
        (select this to remove any https links) ?</td>
      <td><input id="tsync_httpsremove" name="tsync_httpsremove" type="checkbox" value="Y" <?php echo $tsync_httpsremove; ?> /></td>
    </tr>
    
    <tr><td><input id="tsyncbtnUpdate" name="tsyncbtnUpdate" type='submit' value="Sync Template" class="button-primary"/></td><td></td></tr>
    
    </table>
    
    </form>
    
    <p>&nbsp;</p><p>&nbsp;</p>
    <table cellpadding="5">
    <tr><td><strong>Credits</strong></td></tr>
    
    <tr>
      <td>Original code by Shannon Whitley: <a href="http://voiceoftech.com/swhitley/" target="_blank">http://voiceoftech.com/swhitley/</a></td></tr>
    <tr>
      <td>Updated by Solaceten June 2012: <a href="http://www.solaceten.info" target="_blank">http://www.solaceten.info</a></td></tr>
    </table>
    
    </div>
    
    <?php
    
    }
    
    ?>

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic