plugin-yoast.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /* global YoastSEO */
  2. class BeaverBuilderYoast {
  3. constructor() {
  4. // Ensure YoastSEO.js is present and can access the necessary features.
  5. if ( typeof YoastSEO === "undefined" || typeof YoastSEO.analysis === "undefined" || typeof YoastSEO.analysis.worker === "undefined" ) {
  6. return;
  7. }
  8. YoastSEO.app.registerPlugin( "beaverPlugin", { status: "ready" } );
  9. this.registerModifications();
  10. }
  11. /**
  12. * Registers the addContent modification.
  13. *
  14. * @returns {void}
  15. */
  16. registerModifications() {
  17. const callback = this.addContent.bind( this );
  18. // Ensure that the additional data is being seen as a modification to the content.
  19. YoastSEO.app.registerModification( "content", callback, "beaverPlugin", 10 );
  20. }
  21. /**
  22. * Adds to the content to be analyzed by the analyzer.
  23. *
  24. * @param {string} data The current data string.
  25. *
  26. * @returns {string} The data string parameter with the added content.
  27. */
  28. addContent( data ) {
  29. return window.bb_seo_data.content ;
  30. }
  31. }
  32. /**
  33. * Adds eventlistener to load the plugin.
  34. */
  35. if ( typeof YoastSEO !== "undefined" && typeof YoastSEO.app !== "undefined" ) {
  36. new BeaverBuilderYoast();
  37. } else {
  38. jQuery( window ).on(
  39. "YoastSEO:ready",
  40. function() {
  41. new BeaverBuilderYoast();
  42. }
  43. );
  44. }