function motopressVisualEditor()
{
    global $motopressSettings;
    $lang = getLanguageDict();
    wp_register_style('style', plugin_dir_url(__FILE__) . 'includes/css/style.css', null, $motopressSettings['plugin_version']);
    wp_enqueue_style('style');
    $copyErrors = copyParentFiles();
    if (!empty($copyErrors)) {
        Flash::setFlash($copyErrors, 'error');
    } else {
        $initTemplate = new InitTemplate();
        $initErrors = $initTemplate->identify();
        if (!empty($initErrors)) {
            Flash::setFlash($initErrors, 'error');
        } else {
            ?>

            <div class="navbar motopress-navbar">
                <div class="navbar-inner">
                    <div id="motopress-logo">
                        <img src="<?php 
            echo $motopressSettings['plugin_root_url'] . '/' . $motopressSettings['plugin_name'] . '/images/logo.png?ver=' . $motopressSettings['plugin_version'];
            ?>
" />
                    </div>
                    <div id="motopress-editor-group">
                        <div class="pull-left navbar-form motopress-navbar">
                            <ul class="nav">
                                <li>
                                    <span><?php 
            echo $lang->page;
            ?>
&nbsp;</span>
                                    <?php 
            require_once 'includes/showPageList.php';
            ?>

                                </li>
                                <li id="motopress-page-templates-wrapper">
                                    <!--<span id="motopress-page-templates-wrapper">-->
                                        <span><?php 
            echo $lang->template;
            ?>
&nbsp;</span>
                                        <?php 
            require_once 'includes/showTemplateList.php';
            ?>

                                    <!--</span>-->
                                    <button class="btn-default" id="motopress-duplicate-template" data-toggle="modal" data-target="#motopress-duplicate-modal"><?php 
            echo $lang->duplicate;
            ?>
</button>
                                </li>
                            </ul>
                        </div>
                    </div>
                    <div id="motopress-preview-group" style="display: none">
                        <button class="btn-default pull-left active" data-toggle="button" id="motopress-show-hide-blocks">
                            <i class="icon-eye-open"></i>&nbsp;<span><?php 
            echo $lang->showHiddenBlocks;
            ?>
</span>
                        </button>
                    </div>

                    <div class="pull-right">
                        <ul class="nav pull-right">
                            <li>
                                <button class="btn-blue" id="motopress-save"><?php 
            echo $lang->save;
            ?>
</button>
                                <!--<button class="btn-red" id="motopress-reset"><?php 
            //echo $lang->reset;
            ?>
</button>-->
                                <button class="btn-default" id="motopress-visit-site"><?php 
            echo $lang->visitSite;
            ?>
</button>
                            </li>
                            <li class="dropdown" id="screenViews">
                                <a href="javascript:void(0);" class="dropdown-toggle" data-toggle="dropdown">
                                    <div id="currentViewMode" class="screen-views-icon screen-views-editor-icon"></div>
                                    <b class="caret"></b>
                                </a>
                                <ul class="dropdown-menu">
                                    <li><a href="javascript:void(0);" id="defaultScreenView"><div class="screen-views-icon screen-views-desktop-icon"></div></a><hr/></li>
                                    <li><a href="javascript:void(0);" id="tabletScreenView"><div class="screen-views-icon screen-views-tablet-icon"></div></a><hr/></li>
                                    <li><a href="javascript:void(0);" id="phoneScreenView"><div class="screen-views-icon screen-views-phone-icon"></div></a><hr/></li>
                                    <li><a href="javascript:void(0);" id="editorView"><div class="screen-views-icon screen-views-editor-icon"></div></a></li>
                                </ul>
                            </li>
                        </ul>
                    </div>
                </div>
            </div>

            <div id="motopress-flash"></div>

            <div id="motopress-iframe-wrapper">
                <iframe id="motopress-iframe"></iframe>
            </div>

            <div id="motopress-preview-iframe-wrapper">
                <iframe id="motopress-preview-iframe"></iframe>
            </div>

            <!-- Welcome -->
            <div id="motopress-welcome-modal" class="modal hide fade" role="dialog" aria-labelledby="welcomeModalLabel" aria-hidden="true">
                <div class="modal-header">
                    <div class="motopress-close motopress-icon-remove" data-dismiss="modal" aria-hidden="true"></div>
                    <p id="welcomeModalLabel"><?php 
            echo $lang->welcomeToMotopressTitle;
            ?>
</p>
                </div>
                <div class="modal-body">
                    <?php 
            echo $lang->welcomeToMotopressMessage;
            ?>

                </div>
                <div class="modal-footer">
                    <button class="btn-default" data-dismiss="modal" aria-hidden="true"><?php 
            echo $lang->close;
            ?>
</button>
                </div>
            </div>

            <!-- Dublicate -->
            <div id="motopress-duplicate-modal" class="modal hide fade" role="dialog" aria-labelledby="duplicateModalLabel" aria-hidden="true">
                <div class="modal-header">
                    <div class="motopress-close motopress-icon-remove" data-dismiss="modal" aria-hidden="true"></div>
                    <p id="duplicateModalLabel"><?php 
            echo $lang->newTemplateName;
            ?>
</p>
                </div>
                <div class="modal-body">
                    <input type="text" id="motopress-new-template-name" placeholder="<?php 
            echo $lang->newTemplateName;
            ?>
" required maxlength="30">
                </div>
                <div class="modal-footer">
                    <button class="btn-blue" id="motopress-duplicate-template-create"><?php 
            echo $lang->create;
            ?>
</button>
                    <button class="btn-default" data-dismiss="modal" aria-hidden="true"><?php 
            echo $lang->cancel;
            ?>
</button>
                </div>
            </div>

            <!-- Static editor -->
            <div id="motopress-static-editor-modal" class="modal hide fade" role="dialog" aria-labelledby="staticModalLabel" aria-hidden="true">
                <div class="modal-header">
                    <div class="motopress-close motopress-icon-remove" data-dismiss="modal" aria-hidden="true"></div>
                    <p id="staticModalLabel"><?php 
            echo $lang->editContent;
            ?>
</p>
                </div>
                <div class="modal-body">
                    <label for="motopress-static-name" class="blockName-label"><?php 
            echo $lang->staticName;
            ?>
:*</label>
                    <input id="motopress-static-name" type="text" placeholder="<?php 
            echo $lang->staticName;
            ?>
" required maxlength="30">
                    <div id="motopress-static-editor-wrapper">
                        <?php 
            if (isset($_COOKIE['wp-settings-1'])) {
                $_COOKIE['wp-settings-1'] = preg_replace('/editor=(tinymce|html)/is', 'editor=html', $_COOKIE['wp-settings-1'], 1);
            }
            wp_editor('', 'motopress-static-content', array('remove_linebreaks' => 'false', 'schema' => 'html5'));
            ?>

                    </div>
                </div>
                <div class="modal-footer">
                    <button id="motopress-save-static-content" class="btn-blue"><?php 
            echo $lang->save;
            ?>
</button>
                    <button class="btn-default" data-dismiss="modal" aria-hidden="true"><?php 
            echo $lang->cancel;
            ?>
</button>
                </div>
            </div>

            <!-- Confirm -->
            <div id="motopress-confirm-modal" class="modal hide fade" role="dialog" aria-labelledby="confirmModalLabel" aria-hidden="true"></div>

            <!-- Preload -->
            <div id="motopress-preload"></div>

            <script type="text/javascript">
                var steal = { production: 'mp/production.js?ver=<?php 
            echo $motopressSettings['plugin_version'];
            ?>
' };
            </script>
<?php 
            $protocol = !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443 ? 'https' : 'http';
            $wpIncludesUrl = str_replace($protocol . '://' . $_SERVER['HTTP_HOST'], '', includes_url());
            wp_enqueue_script('steal', plugins_url('steal/steal.production.js?mp', __FILE__), null, null);
            wp_localize_script('steal', 'motopress', array('ajaxUrl' => admin_url('admin-ajax.php'), 'wpJQueryUrl' => $wpIncludesUrl . 'js/jquery/', 'wpCssUrl' => $wpIncludesUrl . 'css/', 'pluginVersion' => $motopressSettings['plugin_version'], 'pluginVersionParam' => '?ver=' . $motopressSettings['plugin_version'], 'nonces' => array('motopress_get_wp_settings' => wp_create_nonce('wp_ajax_motopress_get_wp_settings'), 'motopress_get_list' => wp_create_nonce('wp_ajax_motopress_get_list'), 'motopress_get_loop' => wp_create_nonce('wp_ajax_motopress_get_loop'), 'motopress_save' => wp_create_nonce('wp_ajax_motopress_save'), 'motopress_reset' => wp_create_nonce('wp_ajax_motopress_reset'), 'motopress_set_page_template' => wp_create_nonce('wp_ajax_motopress_set_page_template'), 'motopress_duplicate_template' => wp_create_nonce('wp_ajax_motopress_duplicate_template'), 'motopress_save_static_content' => wp_create_nonce('wp_ajax_motopress_save_static_content'), 'motopress_get_static_content' => wp_create_nonce('wp_ajax_motopress_get_static_content'), 'motopress_get_static_block' => wp_create_nonce('wp_ajax_motopress_get_static_block'), 'motopress_get_sidebar' => wp_create_nonce('wp_ajax_motopress_get_sidebar'), 'motopress_get_wrapper' => wp_create_nonce('wp_ajax_motopress_get_wrapper'))));
        }
    }
}
function motopressDuplicateTemplate()
{
    require_once 'verifyNonce.php';
    require_once 'settings.php';
    require_once 'access.php';
    require_once ABSPATH . '/wp-admin/includes/theme.php';
    require_once 'InitTemplate.php';
    require_once 'functions.php';
    require_once 'getLanguageDict.php';
    $pageId = $_POST['pageId'];
    $templateToDuplicate = $_POST['templateToDuplicate'];
    $newTemplateName = trim($_POST['newTemplateName']);
    global $motopressSettings;
    $lang = getLanguageDict();
    $errors = array();
    $templateDir = $motopressSettings['theme_root'] . '/' . $motopressSettings['current_theme'] . '/';
    if ($pageId && $templateToDuplicate && $newTemplateName) {
        $newTemplateFile = uniqid('page-') . '.php';
        if (!preg_match('/^[^\\*\\/]{1,30}$/is', $newTemplateName)) {
            setError($lang->validationName);
        }
        // If template with new name already exists
        $pageTemplates = get_page_templates();
        foreach ($pageTemplates as $name => $file) {
            if (strcasecmp($newTemplateName, $name) == 0) {
                setError($lang->duplicateErrorTemplateExists);
                //$errors[] = 'Template `'. $name .'` already exists.';
                break;
            }
        }
        if (file_exists($templateDir . $templateToDuplicate)) {
            $oldTemplateFile = file_get_contents($templateDir . $templateToDuplicate);
            $oldTemplateFile = InitTemplate::removePhpComment($oldTemplateFile);
            $oldTemplateFile = InitTemplate::removeEmptyPhp($oldTemplateFile);
            $oldTemplateFile = InitTemplate::reinit($oldTemplateFile, $newTemplateFile, 'main');
            $oldTemplateFile = InitTemplate::setAnnotations($oldTemplateFile, array('template_name' => 'Template Name: ' . $newTemplateName));
            if (!file_put_contents($templateDir . $newTemplateFile, $oldTemplateFile)) {
                setError($lang->duplicateError);
                //$errors[] = 'Error on: file_put_contents("'.$templateDir . $newTemplateFile.'", $oldTemplateFile)';
            }
            if (!update_post_meta($pageId, '_wp_page_template', $newTemplateFile)) {
                unlink($templateDir . $newTemplateFile);
                setError($lang->duplicateError);
                //$errors[] = 'Error on: update_post_meta('.$pageId.', "_wp_page_template", "'.$newTemplateFile.'")';
            }
            $request = array('value' => $newTemplateFile, 'name' => $newTemplateName);
            echo json_encode($request);
        } else {
            $errors[] = strtr($lang->duplicateErrorTemplateNotExist, array('%template%' => $templateDir . $templateToDuplicate));
        }
    } else {
        $errors[] = $lang->duplicateError;
    }
    if (!empty($errors)) {
        if ($motopressSettings['debug']) {
            setError($errors);
        } else {
            setError($lang->duplicateError);
        }
    }
    exit;
}
 private function save()
 {
     foreach ($this->newDoms as $file => &$newDom) {
         $fileContent = $newDom['dom']->saveHTML();
         if (!$fileContent) {
             setError();
         }
         $fileContent = InitTemplate::postFix($fileContent);
         $fileContent = preg_replace('~<(?:!DOCTYPE|/?(?:html|body))[^>]*>\\s*~i', '', $fileContent);
         if ($file == $this->page) {
             $fileContent = '<?php get_header(); ?>' . PHP_EOL . $fileContent . PHP_EOL . '<?php get_footer(); ?>';
         }
         $fileContent = InitTemplate::removeEmptyLines($fileContent);
         $fileContent = InitTemplate::setAnnotations($fileContent, $this->oldDoms[$file]['annotations']);
         if (!file_put_contents($this->templateDir . $file, $fileContent)) {
             setError();
         }
     }
 }