PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/wp-migrate-db-pro/asset/dist/js/common.js

https://gitlab.com/najomie/fit-hippie
JavaScript | 191 lines | 122 code | 25 blank | 44 comment | 23 complexity | 3d27fc91cb19315fbe767d834808f87d MD5 | raw file
  1. // global vars
  2. var wpmdb = wpmdb || {};
  3. wpmdb.common = {
  4. hooks: [],
  5. call_stack: [],
  6. non_fatal_errors: '',
  7. migration_error: false
  8. };
  9. wpmdb.functions = {};
  10. /**
  11. * Toggle proper translated strings based on migration type selected.
  12. *
  13. * To show the properly translated strings for the selected push or pull
  14. * migration type, we need to hide all strings then show the right
  15. * translated strings based on the migration type selected.
  16. *
  17. * @see https://github.com/deliciousbrains/wp-migrate-db-pro/issues/764
  18. *
  19. * @return void
  20. */
  21. function wpmdb_toggle_migration_action_text() {
  22. jQuery( '.action-text' ).hide();
  23. jQuery( '.action-text.' + jQuery( 'input[name=action]:checked' ).val() ).show();
  24. }
  25. /**
  26. * Return the currently selected migration type selected.
  27. *
  28. * @return string Will return `push`, `pull`, or `savefile` for exporting as a file.
  29. */
  30. function wpmdb_migration_type() {
  31. return jQuery( 'input[name=action]:checked' ).val();
  32. }
  33. function wpmdb_call_next_hook() {
  34. if ( !wpmdb.common.call_stack.length ) {
  35. wpmdb.common.call_stack = wpmdb.common.hooks;
  36. }
  37. var func = wpmdb.common.call_stack[ 0 ];
  38. wpmdb.common.call_stack.shift();
  39. func.call( this );
  40. }
  41. function wpmdb_add_commas( number_string ) {
  42. number_string += '';
  43. var number_parts = number_string.split( '.' );
  44. var integer = number_parts[ 0 ];
  45. var decimal = 1 < number_parts.length ? '.' + number_parts[ 1 ] : '';
  46. var rgx = /(\d+)(\d{3})/;
  47. while ( rgx.test( integer ) ) {
  48. integer = integer.replace( rgx, '$1' + ',' + '$2' );
  49. }
  50. return integer + decimal;
  51. }
  52. function wpmdb_parse_json( maybe_json ) {
  53. var json_object = {};
  54. try {
  55. json_object = jQuery.parseJSON( maybe_json );
  56. }
  57. catch ( e ) {
  58. // We simply return false here because the json data itself will never just contain a value of "false"
  59. return false;
  60. }
  61. return json_object;
  62. }
  63. /**
  64. * Global error method for detecting PHP or other errors in AJAX response
  65. *
  66. * @param title - the error title if not a PHP error
  67. * @param code - the error code if not a PHP error
  68. * @param text - the AJAX response text to sniff for errors
  69. * @param jqXHR - optional AJAX object used to enrich the error message
  70. *
  71. * @returns {string} - html error string with view error toggle element
  72. */
  73. function wpmdbGetAjaxErrors( title, code, text, jqXHR ) {
  74. var jsonErrors = false;
  75. var html = '';
  76. var validJson = wpmdb_parse_json( text );
  77. if ( false === validJson ) {
  78. jsonErrors = true;
  79. title = wpmdb_strings.ajax_json_message;
  80. code = '(#144)';
  81. var originalText = text;
  82. text = wpmdb_strings.ajax_json_errors + ' ' + code;
  83. text += '<br><a class="show-errors-toggle" href="#">' + wpmdb_strings.view_error_messages + '</a> ';
  84. text += '<div class="migration-php-errors">' + originalText + '</div>';
  85. }
  86. // Only add local connection issue if php errors (#144) or jqXHR has been provided
  87. if ( jsonErrors || 'undefined' !== jqXHR ) {
  88. html += '<strong>' + title + '</strong>' + ' &mdash; ';
  89. }
  90. // Only add extra error details if not php errors (#144) and jqXHR has been provided
  91. if ( !jsonErrors && 'undefined' !== jqXHR ) {
  92. html += wpmdb_strings.status + ': ' + jqXHR.status + ' ' + jqXHR.statusText;
  93. html += '<br /><br />' + wpmdb_strings.response + ':<br />';
  94. }
  95. // Add code to the end of the error text if not json errors
  96. if ( !jsonErrors ) {
  97. text += ' ' + code;
  98. }
  99. // Finally add the error message to the output
  100. html += text;
  101. return html;
  102. }
  103. wpmdb.preg_quote = function( str, delimiter ) {
  104. // discuss at: http://phpjs.org/functions/preg_quote/
  105. // original by: booeyOH
  106. // improved by: Ates Goral (http://magnetiq.com)
  107. // improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  108. // improved by: Brett Zamir (http://brett-zamir.me)
  109. // bugfixed by: Onno Marsman
  110. // example 1: preg_quote("$40");
  111. // returns 1: '\\$40'
  112. // example 2: preg_quote("*RRRING* Hello?");
  113. // returns 2: '\\*RRRING\\* Hello\\?'
  114. // example 3: preg_quote("\\.+*?[^]$(){}=!<>|:");
  115. // returns 3: '\\\\\\.\\+\\*\\?\\[\\^\\]\\$\\(\\)\\{\\}\\=\\!\\<\\>\\|\\:'
  116. return String( str )
  117. .replace( new RegExp( '[.\\\\+*?\\[\\^\\]$(){}=!<>|:\\' + ( delimiter || '' ) + '-]', 'g' ), '\\$&' );
  118. };
  119. wpmdb.table_is = function( table_prefix, desired_table, given_table ) {
  120. if ( ( table_prefix + desired_table ).toLowerCase() === given_table.toLowerCase() ) {
  121. return true;
  122. }
  123. var escaped_given_table = wpmdb.preg_quote( given_table );
  124. var regex = new RegExp( table_prefix + '([0-9]+)_' + desired_table, 'i' );
  125. var results = regex.exec( escaped_given_table );
  126. return null != results;
  127. };
  128. wpmdb.subsite_for_table = function( table_prefix, table_name ) {
  129. var escaped_table_name = wpmdb.preg_quote( table_name );
  130. var regex = new RegExp( table_prefix + '([0-9]+)_', 'i' );
  131. var results = regex.exec( escaped_table_name );
  132. if ( null === results ) {
  133. return 1;
  134. } else {
  135. return results[ 1 ];
  136. }
  137. };
  138. wpmdb.functions.convertKBSizeToHR = function( size, dec, kbSize, retArray ) {
  139. var retVal, units;
  140. kbSize = kbSize || 1000;
  141. dec = dec || 2;
  142. size = parseInt( size );
  143. if ( kbSize > Math.abs( size ) ) {
  144. retVal = [ size.toFixed( 0 ), 'KB' ];
  145. } else {
  146. units = [ 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB' ];
  147. var u = -1;
  148. do {
  149. size /= kbSize;
  150. ++u;
  151. } while ( Math.abs( size ) >= kbSize && u < units.length - 1 );
  152. retVal = [ Math.round( size * Math.pow( 10, dec ) ) / Math.pow( 10, dec ), units[ u ] ];
  153. }
  154. if ( ! retArray ) {
  155. retVal = retVal[0] + ' ' + retVal[1];
  156. }
  157. return retVal;
  158. };
  159. wpmdb.functions.convertKBSizeToHRFixed = function( size, dec, kbSize ) {
  160. dec = dec || 2;
  161. var hrSizeArray = wpmdb.functions.convertKBSizeToHR( size, dec, kbSize, true );
  162. if ( 'KB' !== hrSizeArray[1] ) {
  163. return hrSizeArray[ 0 ].toFixed( 2 ) + ' ' + hrSizeArray[ 1 ];
  164. }
  165. return hrSizeArray[ 0 ] + ' ' + hrSizeArray[ 1 ];
  166. };