Advertisement
natmiletic

yoast.min.js

Feb 27th, 2025
8
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. /**
  2. * Bricks integration with the Yoast SEO plugin.
  3. *
  4. * NOTE: No real-time updates data are provided to YoastSEO. The Bricks content data is fetched on page load and when the post is save/updated.
  5. *
  6. * @since 1.11
  7. */
  8. class BricksYoastContentData {
  9. constructor() {
  10. // Ensure YoastSEO.js is present and can access the necessary features.
  11. if (
  12. typeof YoastSEO === 'undefined' ||
  13. typeof YoastSEO.analysis === 'undefined' ||
  14. typeof YoastSEO.analysis.worker === 'undefined'
  15. ) {
  16. return
  17. }
  18.  
  19. YoastSEO.app.registerPlugin('BricksYoastContentData', { status: 'ready' })
  20.  
  21. this.registerModifications()
  22. }
  23.  
  24. /**
  25. * Registers the addContent modification.
  26. *
  27. * @returns {void}
  28. */
  29. registerModifications() {
  30. const callback = this.addContent.bind(this)
  31.  
  32. // Ensure that the additional data is being seen as a modification to the content.
  33. // Can check in YoastSEO.app.pluggable.modifications to see if the modification is registered.
  34. // content, title, snippet_title, snippet_meta, primary_category, data_page_title, data_meta_desc, excerpt
  35. YoastSEO.app.registerModification('content', callback, 'BricksYoastContentData', 100)
  36. }
  37.  
  38. /**
  39. * Adds to the content to be analyzed by the analyzer.
  40. *
  41. * @param {string} data The current data string.
  42. *
  43. * @returns {string} The data string parameter with the added content.
  44. */
  45. addContent(data) {
  46. if (window.bricksYoast.contentData !== '') {
  47. return window.bricksYoast.contentData
  48. }
  49. return data
  50. }
  51. }
  52.  
  53. // Function to fetch Bricks content data.
  54. function yoastFetchBricksContent() {
  55. jQuery.ajax({
  56. url: window.bricksYoast.ajaxUrl,
  57. type: 'POST',
  58. data: {
  59. action: 'bricks_get_html_from_content',
  60. nonce: window.bricksYoast.nonce,
  61. postId: window.bricksYoast.postId
  62. },
  63. success: function (res) {
  64. if (res.data.html) {
  65. window.bricksYoast.contentData = res.data.html
  66. }
  67.  
  68. if (
  69. typeof YoastSEO !== 'undefined' &&
  70. typeof YoastSEO.app !== 'undefined' &&
  71. typeof YoastSEO.app.refresh === 'function'
  72. ) {
  73. YoastSEO.app.refresh()
  74. }
  75. },
  76. error: function (err) {
  77. console.error('Error updating content data:', err)
  78. }
  79. })
  80. }
  81.  
  82. // Fetch BricksContent on DOM load.
  83. document.addEventListener('DOMContentLoaded', function () {
  84. if (!window.bricksYoast || !window.bricksYoast.renderWithBricks) {
  85. return
  86. }
  87. yoastFetchBricksContent()
  88. })
  89.  
  90. // Register BricksYoastContentData when YoastSEO is ready.
  91. if (typeof YoastSEO !== 'undefined' && typeof YoastSEO.app !== 'undefined') {
  92. new BricksYoastContentData()
  93. } else {
  94. jQuery(window).on('YoastSEO:ready', function () {
  95. new BricksYoastContentData()
  96. })
  97. }
  98.  
  99. // Listen for post updates. (Gutenberg)
  100. if (
  101. typeof wp !== 'undefined' &&
  102. typeof wp.data !== 'undefined' &&
  103. typeof wp.domReady !== 'undefined' &&
  104. typeof wp.data.select('core/editor') !== 'undefined' &&
  105. typeof wp.data.select('core/editor').isSavingPost === 'function' &&
  106. typeof wp.data.select('core/editor').isAutosavingPost === 'function'
  107. ) {
  108. wp.domReady(() => {
  109. let isPostSaving = false
  110. wp.data.subscribe(() => {
  111. // Not listening to isAutosavingPost()
  112. const currentlySaving =
  113. wp.data.select('core/editor').isSavingPost() ||
  114. wp.data.select('core/editor').isAutosavingPost()
  115.  
  116. if (currentlySaving && !isPostSaving) {
  117. // Post is in the process of being saved
  118. }
  119.  
  120. if (!currentlySaving && isPostSaving) {
  121. // Post has finished saving
  122. yoastFetchBricksContent()
  123. }
  124.  
  125. isPostSaving = currentlySaving
  126. })
  127. })
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement