예제 #1
0
    public static function renderJavaScript()
    {
        $uploadSizeInBytes = min(MainWP_Helper::return_bytes(ini_get('upload_max_filesize')), MainWP_Helper::return_bytes(ini_get('post_max_size')));
        $uploadSize = MainWP_Helper::human_filesize($uploadSizeInBytes);
        ?>
        <div id="mainwp-child_clone_status" title="Restore process"></div>
        <script language="javascript">
            jQuery( document ).on( 'change', '#file', function () {
				var maxSize = <?php 
        echo esc_js($uploadSizeInBytes);
        ?>
;
				var humanSize = '<?php 
        echo esc_js($uploadSize);
        ?>
';

                if ( this.files[0].size > maxSize ) {
                    jQuery( '#filesubmit' ).attr( 'disabled', 'disabled' );
                    alert( 'The selected file is bigger than your maximum allowed filesize. (Maximum: ' + humanSize + ')' );
                }
                else {
                    jQuery( '#filesubmit' ).removeAttr( 'disabled' );
                }
            } );
            jQuery( document ).on( 'click', '#mainwp-child_displayby_sitename', function () {
                jQuery( '#mainwp-child_displayby_url' ).removeClass( 'mainwp-child_action_down' );
                jQuery( this ).addClass( 'mainwp-child_action_down' );
                jQuery( '.mainwp-child_url_label' ).hide();
                jQuery( '.mainwp-child_name_label' ).show();
                return false;
            } );
            jQuery( document ).on( 'click', '#mainwp-child_displayby_url', function () {
                jQuery( '#mainwp-child_displayby_sitename' ).removeClass( 'mainwp-child_action_down' );
                jQuery( this ).addClass( 'mainwp-child_action_down' );
                jQuery( '.mainwp-child_name_label' ).hide();
                jQuery( '.mainwp-child_url_label' ).show();
                return false;
            } );
            jQuery( document ).on( 'click', '.clonesite_select_site_item', function () {
                jQuery( '.clonesite_select_site_item' ).removeClass( 'selected' );
                jQuery( this ).addClass( 'selected' );
            } );

            var pollingCreation = undefined;
            var backupCreationFinished = false;

            var pollingDownloading = undefined;
            var backupDownloadFinished = false;

            handleCloneError = function ( resp ) {
                updateClonePopup( resp.error, true, 'red' );
            };

            updateClonePopup = function ( pText, pShowDate, pColor ) {
                if ( pShowDate == undefined ) pShowDate = true;

                var theDiv = jQuery( '#mainwp-child_clone_status' );
                theDiv.append( '<br /><span style="color: ' + pColor + ';">' + (pShowDate ? cloneDateToHMS( new Date() ) + ' ' : '') + pText + '</span>' );
                theDiv.animate( {scrollTop: theDiv.height() * 2}, 100 );
            };

            cloneDateToHMS = function ( date ) {
                var h = date.getHours();
                var m = date.getMinutes();
                var s = date.getSeconds();
                return '' + (h <= 9 ? '0' + h : h) + ':' + (m <= 9 ? '0' + m : m) + ':' + (s <= 9 ? '0' + s : s);
            };

            var translations = [];
			translations['large_site'] = "<?php 
        esc_html_e('This is a large site (%dMB), the restore process will more than likely fail.', 'mainwp-child');
        ?>
";
			translations['continue_anyway'] = "<?php 
        esc_html_e('Continue Anyway?', 'mainwp-child');
        ?>
";
			translations['creating_backup'] = "<?php 
        esc_html_e('Creating backup on %s expected size: %dMB (estimated time: %d seconds)', 'mainwp-child');
        ?>
";
			translations['backup_created'] = "<?php 
        esc_html_e('Backup created on %s total size to download: %dMB', 'mainwp-child');
        ?>
";
			translations['downloading_backup'] = "<?php 
        esc_html_e('Downloading backup', 'mainwp-child');
        ?>
";
			translations['backup_downloaded'] = "<?php 
        esc_html_e('Backup downloaded', 'mainwp-child');
        ?>
";
			translations['extracting_backup'] = "<?php 
        esc_html_e('Extracting backup and updating your database, this might take a while. Please be patient.', 'mainwp-child');
        ?>
";
			translations['clone_complete'] = "<?php 
        esc_html_e('Cloning process completed successfully!', 'mainwp-child');
        ?>
";

            cloneInitiateBackupCreation = function ( siteId, siteName, size, rand, continueAnyway ) {
                if ( (continueAnyway == undefined) && (size > 256) ) {
                    updateClonePopup( mwp_sprintf( translations['large_site'], size ) + ' <a href="#" class="button continueCloneButton" onClick="cloneInitiateBackupCreation(' + "'" + siteId + "'" + ', ' + "'" + siteName + "'" + ', ' + size + ', ' + "'" + rand + "'" + ', true); return false;">' + translations['continue_anyway'] + '</a>' );
                    return;
                }
                else {
                    jQuery( '.continueCloneButton' ).hide();
                }

                size = size / 2.4; //Guessing how large the zip will be

                //5 mb every 10 seconds
                updateClonePopup( mwp_sprintf( translations['creating_backup'], siteName, size.toFixed( 2 ), (size / 5 * 3).toFixed( 2 ) ) );

                updateClonePopup( '<div id="mainwp-child-clone-create-progress" style="margin-top: 1em !important;"></div>', false );
                jQuery( '#mainwp-child-clone-create-progress' ).progressbar( {value: 0, max: (size * 1024)} );

                var data = {
                    action: 'mainwp-child_clone_backupcreate',
                    siteId: siteId,
                    rand: rand
                };

                jQuery.post( ajaxurl, data, function ( pSiteId, pSiteName ) {
                    return function ( resp ) {
                        backupCreationFinished = true;
                        clearTimeout( pollingCreation );

                        var progressBar = jQuery( '#mainwp-child-clone-create-progress' );
                        progressBar.progressbar( 'value', parseFloat( progressBar.progressbar( 'option', 'max' ) ) );

                        if ( resp.error ) {
                            handleCloneError( resp );
                            return;
                        }
                        updateClonePopup( mwp_sprintf( translations['backup_created'], pSiteName, (resp.size / 1024).toFixed( 2 ) ) );
                        //update view;
                        cloneInitiateBackupDownload( pSiteId, resp.url, resp.size );
                    }
                }( siteId, siteName ), 'json' );

                //Poll for filesize 'till it's complete
                pollingCreation = setTimeout( function () {
                    cloneBackupCreationPolling( siteId, rand );
                }, 1000 );
            };

            cloneBackupCreationPolling = function ( siteId, rand ) {
                if ( backupCreationFinished ) return;

                var data = {
                    action: 'mainwp-child_clone_backupcreatepoll',
                    siteId: siteId,
                    rand: rand
                };

                jQuery.post( ajaxurl, data, function ( pSiteId, pRand ) {
                    return function ( resp ) {
                        if ( backupCreationFinished ) return;
                        if ( resp.size ) {
                            var progressBar = jQuery( '#mainwp-child-clone-create-progress' );
                            if ( progressBar.progressbar( 'option', 'value' ) < progressBar.progressbar( 'option', 'max' ) ) {
                                progressBar.progressbar( 'value', resp.size );
                            }

                            //Also update estimated time?? ETA??
                        }
                        pollingCreation = setTimeout( function () {
                            cloneBackupCreationPolling( pSiteId, pRand );
                        }, 1000 );
                    }
                }( siteId, rand ), 'json' );
            };

            cloneInitiateBackupDownload = function ( pSiteId, pFile, pSize ) {
                updateClonePopup( translations['downloading_backup'] );

                updateClonePopup( '<div id="mainwp-child-clone-download-progress" style="margin-top: 1em !important;"></div>', false );
                jQuery( '#mainwp-child-clone-download-progress' ).progressbar( {value: 0, max: pSize} );

                var data = {
                    action: 'mainwp-child_clone_backupdownload',
                    file: pFile
                };

                if ( pSiteId != undefined ) data['siteId'] = pSiteId;

                jQuery.post( ajaxurl, data, function ( siteId ) {
                    return function ( resp ) {
                        backupDownloadFinished = true;
                        clearTimeout( pollingDownloading );

                        var progressBar = jQuery( '#mainwp-child-clone-download-progress' );
                        progressBar.progressbar( 'value', parseFloat( progressBar.progressbar( 'option', 'max' ) ) );

                        if ( resp.error ) {
                            handleCloneError( resp );
                            return;
                        }
                        updateClonePopup( translations['backup_downloaded'] );

                        //update view
                        cloneInitiateExtractBackup();
                    }
                }( pSiteId ), 'json' );

                //Poll for filesize 'till it's complete
                pollingDownloading = setTimeout( function () {
                    cloneBackupDownloadPolling( pSiteId, pFile );
                }, 1000 );
            };

            cloneBackupDownloadPolling = function ( siteId, pFile ) {
                if ( backupDownloadFinished ) return;

                var data = {
                    action: 'mainwp-child_clone_backupdownloadpoll',
                    siteId: siteId,
                    file: pFile
                };

                jQuery.post( ajaxurl, data, function ( pSiteId ) {
                    return function ( resp ) {
                        if ( backupDownloadFinished ) return;
                        if ( resp.size ) {
                            var progressBar = jQuery( '#mainwp-child-clone-download-progress' );
                            if ( progressBar.progressbar( 'option', 'value' ) < progressBar.progressbar( 'option', 'max' ) ) {
                                progressBar.progressbar( 'value', resp.size );
                            }
                        }

                        pollingDownloading = setTimeout( function () {
                            cloneBackupDownloadPolling( pSiteId );
                        }, 1000 );
                    }
                }( siteId ), 'json' );
            };

            cloneInitiateExtractBackup = function ( file ) {
                if ( file == undefined ) file = '';

                updateClonePopup( translations['extracting_backup'] );
                //Extract & install SQL
                var data = {
                    action: 'mainwp-child_clone_backupextract',
                    f: file
                };

                jQuery.ajax( {
                    type: "POST",
                    url: ajaxurl,
                    data: data,
                    success: function ( resp ) {
                        if ( resp.error ) {
                            handleCloneError( resp );
                            return;
                        }

                        updateClonePopup( translations['clone_complete'] );

                        setTimeout( function () {
                            jQuery( '#mainwp-child_clone_status' ).dialog( 'close' );
                            jQuery( '.mainwp-child_select_sites_box' ).hide();
                            jQuery( '.mainwp-child_info-box-green' ).show();
                            jQuery( '#mainwp-child_uploadclonebutton' ).hide();
                            jQuery( '#mainwp-child_clonebutton' ).hide();
                            jQuery( '.mainwp-hide-after-restore' ).hide();
                        }, 1000 );
                    },
                    dataType: 'json'
                } );
            };

            jQuery( document ).on( 'click', '#mainwp-child-restore', function () {
                jQuery( '#mainwp-child_clone_status' ).dialog( {
                    resizable: false,
                    height: 400,
                    width: 750,
                    modal: true,
                    close: function ( event, ui ) {
                        bulkTaskRunning = false;
                        jQuery( '#mainwp-child_clone_status' ).dialog( 'destroy' );
                    }
                } );

                cloneInitiateBackupDownload( undefined, jQuery( this ).attr( 'file' ), jQuery( this ).attr( 'size' ) );
                return false;
            } );

            jQuery( document ).on( 'click', '#mainwp-child_uploadclonebutton', function () {
                var file = jQuery( this ).attr( 'file' );
                jQuery( '#mainwp-child_clone_status' ).dialog( {
                    resizable: false,
                    height: 400,
                    width: 750,
                    modal: true,
                    close: function ( event, ui ) {
                        bulkTaskRunning = false;
                        jQuery( '#mainwp-child_clone_status' ).dialog( 'destroy' );
                    }
                } );

                cloneInitiateExtractBackup( file );
                return false;
            } );

            jQuery( document ).on( 'click', '#mainwp-child_clonebutton', function () {
                jQuery( '#mainwp-child_clone_status' ).dialog( {
                    resizable: false,
                    height: 400,
                    width: 750,
                    modal: true,
                    close: function ( event, ui ) {
                        bulkTaskRunning = false;
                        jQuery( '#mainwp-child_clone_status' ).dialog( 'destroy' );
                    }
                } );

                //Initiate backup creation on other child
                var siteElement = jQuery( '.clonesite_select_site_item.selected' );
                var siteId = siteElement.attr( 'id' );
                var siteName = siteElement.find( '.mainwp-child_name_label' ).html();
                var siteSize = siteElement.find( '.mainwp-child_size_label' ).attr( 'size' );
                var siteRand = siteElement.attr( 'rand' );
                cloneInitiateBackupCreation( siteId, siteName, siteSize, siteRand );

                return false;
            } );

            function mwp_sprintf() {
                if ( !arguments || arguments.length < 1 || !RegExp ) {
                    return;
                }
                var str = arguments[0];
                var re = /([^%]*)%('.|0|\x20)?(-)?(\d+)?(\.\d+)?(%|b|c|d|u|f|o|s|x|X)(.*)/;
                var a = b = [], numSubstitutions = 0, numMatches = 0;
                while ( a = re.exec( str ) ) {
                    var leftpart = a[1], pPad = a[2], pJustify = a[3], pMinLength = a[4];
                    var pPrecision = a[5], pType = a[6], rightPart = a[7];

                    //alert(a + '\n' + [a[0], leftpart, pPad, pJustify, pMinLength, pPrecision);

                    numMatches++;
                    if ( pType == '%' ) {
                        subst = '%';
                    }
                    else {
                        numSubstitutions++;
                        if ( numSubstitutions >= arguments.length ) {
                            alert( 'Error! Not enough function arguments (' + (arguments.length - 1) + ', excluding the string)\nfor the number of substitution parameters in string (' + numSubstitutions + ' so far).' );
                        }
                        var param = arguments[numSubstitutions];
                        var pad = '';
                        if ( pPad && pPad.substr( 0, 1 ) == "'" ) pad = leftpart.substr( 1, 1 );
                        else if ( pPad ) pad = pPad;
                        var justifyRight = true;
                        if ( pJustify && pJustify === "-" ) justifyRight = false;
                        var minLength = -1;
                        if ( pMinLength ) minLength = parseInt( pMinLength );
                        var precision = -1;
                        if ( pPrecision && pType == 'f' ) precision = parseInt( pPrecision.substring( 1 ) );
                        var subst = param;
                        if ( pType == 'b' ) subst = parseInt( param ).toString( 2 );
                        else if ( pType == 'c' ) subst = String.fromCharCode( parseInt( param ) );
                        else if ( pType == 'd' ) subst = parseInt( param ) ? parseInt( param ) : 0;
                        else if ( pType == 'u' ) subst = Math.abs( param );
                        else if ( pType == 'f' ) subst = (precision > -1) ? Math.round( parseFloat( param ) * Math.pow( 10, precision ) ) / Math.pow( 10, precision ) : parseFloat( param );
                        else if ( pType == 'o' ) subst = parseInt( param ).toString( 8 );
                        else if ( pType == 's' ) subst = param;
                        else if ( pType == 'x' ) subst = ('' + parseInt( param ).toString( 16 )).toLowerCase();
                        else if ( pType == 'X' ) subst = ('' + parseInt( param ).toString( 16 )).toUpperCase();
                    }
                    str = leftpart + subst + rightPart;
                }
                return str;
            }

            jQuery( document ).on( 'click', '#mainwp-child_clonebutton_from_server', function () {
                var cur_dir = jQuery( '#clonesite_from_server_current_dir' ).val();
                var file = cur_dir + '/' + jQuery( '.clonesite_select_site_item.selected span' ).html();
                jQuery( '#mainwp-child_clone_status' ).dialog( {
                    resizable: false,
                    height: 400,
                    width: 750,
                    modal: true,
                    close: function ( event, ui ) {
                        bulkTaskRunning = false;
                        jQuery( '#mainwp-child_clone_status' ).dialog( 'destroy' );
                    }
                } );

                cloneInitiateExtractBackup( file );
                return false;
            } );

        </script>
		<?php 
        self::renderFooter();
    }