PageRenderTime 57ms CodeModel.GetById 4ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/broken-link-checker/includes/admin/links-page-js.php

https://bitbucket.org/lgorence/quickpress
PHP | 670 lines | 520 code | 79 blank | 71 comment | 75 complexity | d37c963d8e8a5d401d677b8338c68130 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, AGPL-1.0
  1. <script type='text/javascript'>
  2. function alterLinkCounter(factor){
  3. var cnt = parseInt(jQuery('.current-link-count').eq(0).html());
  4. cnt = cnt + factor;
  5. jQuery('.current-link-count').html(cnt);
  6. if ( blc_is_broken_filter ){
  7. //Update the broken link count displayed beside the "Broken Links" menu
  8. var menuBubble = jQuery('span.blc-menu-bubble');
  9. if ( menuBubble.length > 0 ){
  10. cnt = parseInt(menuBubble.eq(0).html());
  11. cnt = cnt + factor;
  12. if ( cnt > 0 ){
  13. menuBubble.html(cnt);
  14. } else {
  15. menuBubble.parent().hide();
  16. }
  17. }
  18. }
  19. }
  20. function replaceLinkId(old_id, new_id){
  21. var master = jQuery('#blc-row-'+old_id);
  22. master.attr('id', 'blc-row-'+new_id);
  23. master.find('.blc-link-id').html(new_id);
  24. var details_row = jQuery('#link-details-'+old_id);
  25. details_row.attr('id', 'link-details-'+new_id);
  26. }
  27. function reloadDetailsRow(link_id){
  28. var details_row = jQuery('#link-details-'+link_id);
  29. //Load up the new link info (so sue me)
  30. details_row.find('td').html('<center><?php echo esc_js(__('Loading...' , 'broken-link-checker')); ?></center>').load(
  31. "<?php echo admin_url('admin-ajax.php'); ?>",
  32. {
  33. 'action' : 'blc_link_details',
  34. 'link_id' : link_id
  35. }
  36. );
  37. }
  38. jQuery(function($){
  39. //The details button - display/hide detailed info about a link
  40. $(".blc-details-button, td.column-link-text, td.column-status, td.column-new-link-text").click(function () {
  41. var master = $(this).parents('.blc-row');
  42. var link_id = master.attr('id').split('-')[2];
  43. $('#link-details-'+link_id).toggle();
  44. });
  45. var ajaxInProgressHtml = '<?php echo esc_js(__('Wait...', 'broken-link-checker')); ?>';
  46. //The "Not broken" button - manually mark the link as valid. The link will be checked again later.
  47. $(".blc-discard-button").click(function () {
  48. var me = $(this);
  49. me.html(ajaxInProgressHtml);
  50. var master = me.parents('.blc-row');
  51. var link_id = master.attr('id').split('-')[2];
  52. $.post(
  53. "<?php echo admin_url('admin-ajax.php'); ?>",
  54. {
  55. 'action' : 'blc_discard',
  56. 'link_id' : link_id,
  57. '_ajax_nonce' : '<?php echo esc_js(wp_create_nonce('blc_discard')); ?>'
  58. },
  59. function (data, textStatus){
  60. if (data == 'OK'){
  61. var details = $('#link-details-'+link_id);
  62. //Remove the "Not broken" action
  63. me.parent().remove();
  64. //Set the displayed link status to OK
  65. var classNames = master.attr('class');
  66. classNames = classNames.replace(/(^|\s)link-status-[^\s]+(\s|$)/, ' ') + ' link-status-ok';
  67. master.attr('class', classNames);
  68. //Flash the main row green to indicate success, then remove it if the current view
  69. //is supposed to show only broken links.
  70. flashElementGreen(master, function(){
  71. if ( blc_is_broken_filter ){
  72. details.remove();
  73. master.remove();
  74. } else {
  75. reloadDetailsRow(link_id);
  76. }
  77. });
  78. //Update the elements displaying the number of results for the current filter.
  79. if( blc_is_broken_filter ){
  80. alterLinkCounter(-1);
  81. }
  82. } else {
  83. me.html('<?php echo esc_js(__('Not broken' , 'broken-link-checker')); ?>');
  84. alert(data);
  85. }
  86. }
  87. );
  88. return false;
  89. });
  90. //The "Dismiss" button - hide the link from the "Broken" and "Redirects" filters, but still apply link tweaks and so on.
  91. $(".blc-dismiss-button").click(function () {
  92. var me = $(this);
  93. var oldButtonHtml = me.html();
  94. me.html(ajaxInProgressHtml);
  95. var master = me.closest('.blc-row');
  96. var link_id = master.attr('id').split('-')[2];
  97. var should_hide_link = (blc_current_base_filter == 'broken') || (blc_current_base_filter == 'redirects');
  98. $.post(
  99. "<?php echo admin_url('admin-ajax.php'); ?>",
  100. {
  101. 'action' : 'blc_dismiss',
  102. 'link_id' : link_id,
  103. '_ajax_nonce' : '<?php echo esc_js(wp_create_nonce('blc_dismiss')); ?>'
  104. },
  105. function (data, textStatus){
  106. if (data == 'OK'){
  107. var details = $('#link-details-'+link_id);
  108. //Remove the "Dismiss" action
  109. me.parent().hide();
  110. //Flash the main row green to indicate success, then remove it if necessary.
  111. flashElementGreen(master, function(){
  112. if ( should_hide_link ){
  113. details.remove();
  114. master.remove();
  115. }
  116. });
  117. //Update the elements displaying the number of results for the current filter.
  118. if( should_hide_link ){
  119. alterLinkCounter(-1);
  120. }
  121. } else {
  122. me.html(oldButtonHtml);
  123. alert(data);
  124. }
  125. }
  126. );
  127. return false;
  128. });
  129. //The "Undismiss" button.
  130. $(".blc-undismiss-button").click(function () {
  131. var me = $(this);
  132. var oldButtonHtml = me.html();
  133. me.html(ajaxInProgressHtml);
  134. var master = me.closest('.blc-row');
  135. var link_id = master.attr('id').split('-')[2];
  136. var should_hide_link = (blc_current_base_filter == 'dismissed');
  137. $.post(
  138. "<?php echo admin_url('admin-ajax.php'); ?>",
  139. {
  140. 'action' : 'blc_undismiss',
  141. 'link_id' : link_id,
  142. '_ajax_nonce' : '<?php echo esc_js(wp_create_nonce('blc_undismiss')); ?>'
  143. },
  144. function (data, textStatus){
  145. if (data == 'OK'){
  146. var details = $('#link-details-'+link_id);
  147. //Remove the action.
  148. me.parent().hide();
  149. //Flash the main row green to indicate success, then remove it if necessary.
  150. flashElementGreen(master, function(){
  151. if ( should_hide_link ){
  152. details.remove();
  153. master.remove();
  154. }
  155. });
  156. //Update the elements displaying the number of results for the current filter.
  157. if( should_hide_link ){
  158. alterLinkCounter(-1);
  159. }
  160. } else {
  161. me.html(oldButtonHtml);
  162. alert(data);
  163. }
  164. }
  165. );
  166. return false;
  167. });
  168. function flashElementGreen(element, callback) {
  169. var oldColor = element.css('background-color');
  170. element.animate({ backgroundColor: "#E0FFB3" }, 200).animate({ backgroundColor: oldColor }, 300, callback);
  171. }
  172. /**
  173. * Display the inline URL editor for a particular link (that's present in the current view).
  174. *
  175. * @param link_id Either a link ID (int), or a jQuery object representing the link row.
  176. */
  177. function showUrlEditor(link_id){
  178. var master;
  179. if ( isNaN(link_id) ){
  180. master = link_id;
  181. } else {
  182. master = $('#blc-row-' + link_id);
  183. }
  184. var url_el = master.find('a.blc-link-url').hide();
  185. master.find('div.blc-url-editor-buttons').show();
  186. master.find('input.blc-link-editor')
  187. .val( url_el.attr('href') ).show().focus().select()
  188. .parents('td').find('div.row-actions').hide();
  189. }
  190. /**
  191. * Hide the URL editor of a particular link.
  192. *
  193. * @param link_id Either a link ID (int), or a jQuery object representing the link row.
  194. */
  195. function hideUrlEditor(link_id){
  196. var master;
  197. if ( isNaN(link_id) ){
  198. master = link_id;
  199. } else {
  200. master = $('#blc-row-' + link_id);
  201. }
  202. master.find('div.blc-url-editor-buttons').hide();
  203. master.find('input.blc-link-editor').hide();
  204. master.find('a.blc-link-url').show();
  205. master.find('div.row-actions').show();
  206. }
  207. /**
  208. * Call our PHP backend and tell it to edit all occurences of particular link so that they
  209. * point to a different URL. Updates UI with the new link info and displays any error messages
  210. * that might be generated.
  211. *
  212. * @param link_id Either a link ID (int), or a jQuery object representing the link row.
  213. * @param new_url The new URL for the link.
  214. */
  215. function updateLinkUrl(link_id, new_url){
  216. var master;
  217. if ( isNaN(link_id) ){
  218. master = link_id;
  219. link_id = master.attr('id').split("-")[2]; //id="blc-row-$linkid"
  220. } else {
  221. master = $('#blc-row-' + link_id);
  222. }
  223. var url_el = master.find('a.blc-link-url');
  224. var orig_url = url_el.attr('href');
  225. var progress_indicator = master.find('.waiting');
  226. progress_indicator.show();
  227. $.getJSON(
  228. "<?php echo admin_url('admin-ajax.php'); ?>",
  229. {
  230. 'action' : 'blc_edit',
  231. 'link_id' : link_id,
  232. 'new_url' : new_url,
  233. '_ajax_nonce' : '<?php echo esc_js(wp_create_nonce('blc_edit')); ?>'
  234. },
  235. function (data, textStatus){
  236. var display_url = '';
  237. if ( data && (typeof(data['error']) != 'undefined') ){
  238. //An internal error occured before the link could be edited.
  239. //data.error is an error message.
  240. alert(data.error);
  241. display_url = orig_url;
  242. } else {
  243. //data contains info about the performed edit
  244. if ( data.errors.length == 0 ){
  245. //Everything went well.
  246. //Replace the displayed link URL with the new one.
  247. display_url = new_url;
  248. url_el.attr('href', new_url);
  249. //Save the new ID
  250. replaceLinkId(link_id, data.new_link_id);
  251. //Load up the new link info
  252. reloadDetailsRow(data.new_link_id);
  253. //Remove classes indicating link state - they're probably wrong by now
  254. var classNames = master.attr('class').replace(/(^|\s)link-status-[^\s]+(\s|$)/, ' ')+' link-status-unknown';
  255. master.attr('class', classNames);
  256. master.removeClass('blc-redirect');
  257. //Flash the row green to indicate success
  258. var oldColor = master.css('background-color');
  259. master.animate({ backgroundColor: "#E0FFB3" }, 200).animate({ backgroundColor: oldColor }, 300);
  260. } else {
  261. display_url = orig_url;
  262. //Build and display an error message.
  263. var msg = '';
  264. if ( data.cnt_okay > 0 ){
  265. var msgpiece = sprintf(
  266. '<?php echo esc_js(__('%d instances of the link were successfully modified.', 'broken-link-checker')); ?>',
  267. data.cnt_okay
  268. );
  269. msg = msg + msgpiece + '\n';
  270. if ( data.cnt_error > 0 ){
  271. msgpiece = sprintf(
  272. '<?php echo esc_js(__("However, %d instances couldn't be edited and still point to the old URL.", 'broken-link-checker')); ?>',
  273. data.cnt_error
  274. );
  275. msg = msg + msgpiece + "\n";
  276. }
  277. } else {
  278. msg = msg + '<?php echo esc_js(__('The link could not be modified.', 'broken-link-checker')); ?>\n';
  279. }
  280. msg = msg + '\n<?php echo esc_js(__("The following error(s) occured :", 'broken-link-checker')); ?>\n* ';
  281. msg = msg + data.errors.join('\n* ');
  282. alert(msg);
  283. }
  284. }
  285. url_el.text(display_url);
  286. progress_indicator.hide();
  287. hideUrlEditor(master);
  288. }
  289. );
  290. }
  291. //The "Edit URL" button - displays the inline editor
  292. $(".blc-edit-button").click(function () {
  293. var edit_button = $(this);
  294. var master = $(edit_button).parents('.blc-row');
  295. var editor = $(master).find('input.blc-link-editor');
  296. var url_el = $(master).find('.blc-link-url');
  297. //Find the current/original URL
  298. var orig_url = url_el.attr('href');
  299. //Find the link ID
  300. var link_id = master.attr('id').split('-')[2];
  301. if ( !editor.is(':visible') ){
  302. showUrlEditor(link_id);
  303. } else {
  304. hideUrlEditor(link_id);
  305. }
  306. });
  307. //The "Update URL" button in the inline editor
  308. $('.blc-update-url-button').click(function(){
  309. var master = $(this).parents('tr.blc-row');
  310. var url_el = master.find('.blc-link-url');
  311. var editor = master.find('input.blc-link-editor');
  312. var old_url = url_el.attr('href');
  313. var new_url = editor.val();
  314. if ( (new_url == old_url) || ($.trim(new_url) == '') ){
  315. hideUrlEditor(master);
  316. } else {
  317. updateLinkUrl(master, new_url);
  318. }
  319. });
  320. //Let the user use Enter and Esc as shortcuts for "Update URL" and "Cancel"
  321. $('input.blc-link-editor').keypress(function (e) {
  322. if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
  323. $(this).parents('.blc-row').find('.blc-update-url-button').click();
  324. return false;
  325. } else if ((e.which && e.which == 27) || (e.keyCode && e.keyCode == 27)) {
  326. $(this).parents('.blc-row').find('.blc-cancel-button').click();
  327. return false;
  328. } else {
  329. return true;
  330. }
  331. });
  332. //The "Cancel" in the inline editor
  333. $(".blc-cancel-button").click(function () {
  334. var master = $(this).parents('.blc-row');
  335. hideUrlEditor(master);
  336. });
  337. //The "Unlink" button - remove the link/image from all posts, custom fields, etc.
  338. $(".blc-unlink-button").click(function () {
  339. var me = this;
  340. var master = $(me).parents('.blc-row');
  341. $(me).html('<?php echo esc_js(__('Wait...' , 'broken-link-checker')); ?>');
  342. //Find the link ID
  343. var link_id = master.attr('id').split('-')[2];
  344. $.post(
  345. "<?php echo admin_url('admin-ajax.php'); ?>",
  346. {
  347. 'action' : 'blc_unlink',
  348. 'link_id' : link_id,
  349. '_ajax_nonce' : '<?php echo esc_js(wp_create_nonce('blc_unlink')); ?>'
  350. },
  351. function (data, textStatus){
  352. eval('data = ' + data);
  353. if ( data && (typeof(data['error']) != 'undefined') ){
  354. //An internal error occured before the link could be edited.
  355. //data.error is an error message.
  356. alert(data.error);
  357. } else {
  358. if ( data.errors.length == 0 ){
  359. //The link was successfully removed. Hide its details.
  360. $('#link-details-'+link_id).hide();
  361. //Flash the main row green to indicate success, then hide it.
  362. var oldColor = master.css('background-color');
  363. master.animate({ backgroundColor: "#E0FFB3" }, 200).animate({ backgroundColor: oldColor }, 300, function(){
  364. master.hide();
  365. });
  366. alterLinkCounter(-1);
  367. return;
  368. } else {
  369. //Build and display an error message.
  370. var msg = '';
  371. if ( data.cnt_okay > 0 ){
  372. msg = msg + sprintf(
  373. '<?php echo esc_js(__("%d instances of the link were successfully unlinked.", 'broken-link-checker')); ?>\n',
  374. data.cnt_okay
  375. );
  376. if ( data.cnt_error > 0 ){
  377. msg = msg + sprintf(
  378. '<?php echo esc_js(__("However, %d instances couldn't be removed.", 'broken-link-checker')); ?>\n',
  379. data.cnt_error
  380. );
  381. }
  382. } else {
  383. msg = msg + '<?php echo esc_js(__("The plugin failed to remove the link.", 'broken-link-checker')); ?>\n';
  384. }
  385. msg = msg + '\n<?php echo esc_js(__("The following error(s) occured :", 'broken-link-checker')); ?>\n* ';
  386. msg = msg + data.errors.join('\n* ');
  387. //Show the error message
  388. alert(msg);
  389. }
  390. }
  391. $(me).html('<?php echo esc_js(__('Unlink' , 'broken-link-checker')); ?>');
  392. }
  393. );
  394. });
  395. //--------------------------------------------
  396. //The search box(es)
  397. //--------------------------------------------
  398. var searchForm = $('#search-links-dialog');
  399. searchForm.dialog({
  400. autoOpen : false,
  401. dialogClass : 'blc-search-container',
  402. resizable: false
  403. });
  404. $('#blc-open-search-box').click(function(){
  405. if ( searchForm.dialog('isOpen') ){
  406. searchForm.dialog('close');
  407. } else {
  408. //Display the search form under the "Search" button
  409. var button_position = $('#blc-open-search-box').offset();
  410. var button_height = $('#blc-open-search-box').outerHeight(true);
  411. var button_width = $('#blc-open-search-box').outerWidth(true);
  412. var dialog_width = searchForm.dialog('option', 'width');
  413. searchForm.dialog('option', 'position',
  414. [
  415. button_position.left - dialog_width + button_width/2,
  416. button_position.top + button_height + 1 - $(document).scrollTop()
  417. ]
  418. );
  419. searchForm.dialog('open');
  420. }
  421. });
  422. $('#blc-cancel-search').click(function(){
  423. searchForm.dialog('close');
  424. });
  425. //The "Save This Search Query" button creates a new custom filter based on the current search
  426. $('#blc-create-filter').click(function(){
  427. var filter_name = prompt("<?php echo esc_js(__("Enter a name for the new custom filter", 'broken-link-checker')); ?>", "");
  428. if ( filter_name ){
  429. $('#blc-custom-filter-name').val(filter_name);
  430. $('#custom-filter-form').submit();
  431. }
  432. });
  433. //Display a confirmation dialog when the user clicks the "Delete This Filter" button
  434. $('#blc-delete-filter').click(function(){
  435. var message = '<?php
  436. echo esc_js(
  437. __("You are about to delete the current filter.\n'Cancel' to stop, 'OK' to delete", 'broken-link-checker')
  438. );
  439. ?>';
  440. return confirm(message);
  441. });
  442. //--------------------------------------------
  443. // Bulk actions
  444. //--------------------------------------------
  445. $('#blc-bulk-action-form').submit(function(){
  446. var action = $('#blc-bulk-action').val(), message;
  447. if ( action == '-1' ){
  448. action = $('#blc-bulk-action2').val();
  449. }
  450. if ( action == 'bulk-delete-sources' ){
  451. //Convey the gravitas of deleting link sources.
  452. message = '<?php
  453. echo esc_js(
  454. __("Are you sure you want to delete all posts, bookmarks or other items that contain any of the selected links? This action can't be undone.\n'Cancel' to stop, 'OK' to delete", 'broken-link-checker')
  455. );
  456. ?>';
  457. if ( !confirm(message) ){
  458. return false;
  459. }
  460. } else if ( action == 'bulk-unlink' ){
  461. //Likewise for unlinking.
  462. message = '<?php
  463. echo esc_js(
  464. __("Are you sure you want to remove the selected links? This action can't be undone.\n'Cancel' to stop, 'OK' to remove", 'broken-link-checker')
  465. );
  466. ?>';
  467. if ( !confirm(message) ){
  468. return false;
  469. }
  470. }
  471. });
  472. //------------------------------------------------------------
  473. // Manipulate highlight settings for permanently broken links
  474. //------------------------------------------------------------
  475. var highlight_permanent_failures_checkbox = $('#highlight_permanent_failures');
  476. var failure_duration_threshold_input = $('#failure_duration_threshold');
  477. //Apply/remove highlights when the checkbox is (un)checked
  478. highlight_permanent_failures_checkbox.change(function(){
  479. //save_highlight_settings();
  480. if ( this.checked ){
  481. $('#blc-links tr.blc-permanently-broken').addClass('blc-permanently-broken-hl');
  482. } else {
  483. $('#blc-links tr.blc-permanently-broken').removeClass('blc-permanently-broken-hl');
  484. }
  485. });
  486. //Apply/remove highlights when the duration threshold is changed.
  487. failure_duration_threshold_input.change(function(){
  488. var new_threshold = parseInt($(this).val());
  489. //save_highlight_settings();
  490. if (isNaN(new_threshold) || (new_threshold < 1)) {
  491. return;
  492. }
  493. highlight_permanent_failures = highlight_permanent_failures_checkbox.is(':checked');
  494. $('#blc-links tr.blc-row').each(function(index){
  495. var days_broken = $(this).attr('data-days-broken');
  496. if ( days_broken >= new_threshold ){
  497. $(this).addClass('blc-permanently-broken');
  498. if ( highlight_permanent_failures ){
  499. $(this).addClass('blc-permanently-broken-hl');
  500. }
  501. } else {
  502. $(this).removeClass('blc-permanently-broken').removeClass('blc-permanently-broken-hl');
  503. }
  504. });
  505. });
  506. //Show/hide table columns dynamically
  507. $('#blc-column-selector input[type="checkbox"]').change(function(){
  508. var checkbox = $(this);
  509. var column_id = checkbox.attr('name').split(/\[|\]/)[1];
  510. if (checkbox.is(':checked')){
  511. $('td.column-'+column_id+', th.column-'+column_id, '#blc-links').removeClass('hidden');
  512. } else {
  513. $('td.column-'+column_id+', th.column-'+column_id, '#blc-links').addClass('hidden');
  514. }
  515. //Recalculate colspan's for detail rows to take into account the changed number of
  516. //visible columns. Otherwise you can get some ugly layout glitches.
  517. $('#blc-links tr.blc-link-details td').attr(
  518. 'colspan',
  519. $('#blc-column-selector input[type="checkbox"]:checked').length+1
  520. );
  521. });
  522. //Unlike other fields in "Screen Options", the links-per-page setting
  523. //is handled using straight form submission (POST), not AJAX.
  524. $('#blc-per-page-apply-button').click(function(){
  525. $('#adv-settings').submit();
  526. });
  527. $('#blc_links_per_page').keypress(function(e){
  528. if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
  529. $('#adv-settings').submit();
  530. }
  531. });
  532. //Toggle status code colors when the corresponding checkbox is toggled
  533. $('#table_color_code_status').click(function(){
  534. if ( $(this).is(':checked') ){
  535. $('#blc-links').addClass('color-code-link-status');
  536. } else {
  537. $('#blc-links').removeClass('color-code-link-status');
  538. }
  539. });
  540. //Show the bulk edit/find & replace form when the user applies the appropriate bulk action
  541. $('#doaction, #doaction2').click(function(e){
  542. var n = $(this).attr('id').substr(2);
  543. if ( $('select[name="'+n+'"]').val() == 'bulk-edit' ) {
  544. e.preventDefault();
  545. //Any links selected?
  546. if ($('tbody th.check-column input:checked').length > 0){
  547. $('#bulk-edit').show();
  548. }
  549. }
  550. });
  551. //Hide the bulk edit/find & replace form when "Cancel" is clicked
  552. $('#bulk-edit .cancel').click(function(){
  553. $('#bulk-edit').hide();
  554. return false;
  555. });
  556. //Minimal input validation for the bulk edit form
  557. $('#bulk-edit input[type="submit"]').click(function(e){
  558. if( $('#bulk-edit input[name="search"]').val() == '' ){
  559. alert('<?php echo esc_js(__('Enter a search string first.', 'broken-link-checker')); ?>');
  560. $('#bulk-edit input[name="search"]').focus();
  561. e.preventDefault();
  562. return;
  563. }
  564. if ($('tbody th.check-column input:checked').length == 0){
  565. alert('<?php echo esc_js(__('Select one or more links to edit.', 'broken-link-checker')); ?>');
  566. e.preventDefault();
  567. }
  568. });
  569. });
  570. </script>