Skip to content

Commit

Permalink
Crowdsignal shortcode: impl review suggestions (#29331)
Browse files Browse the repository at this point in the history
  • Loading branch information
sgomes authored Mar 9, 2023
1 parent 394c1b8 commit cb3eb70
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
24 changes: 13 additions & 11 deletions projects/plugins/jetpack/_inc/polldaddy-shortcode.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
( function ( w, d ) {
function parseJSON( str ) {
try {
return str ? w.JSON.parse( str ) : null;
} catch ( error ) {
return null;
}
}

w.polldaddyshortcode = {
render: function () {
const ratings = d.querySelectorAll( 'div.pd-rating[data-settings]' );
const polls = d.querySelectorAll( 'div.PDS_Poll[data-settings]' );

polls.forEach( pollEl => {
const isInitialized = pollEl.getAttribute( 'data-pd-init-done' );

if ( isInitialized ) {
if ( pollEl.hasAttribute( 'data-pd-init-done' ) ) {
return;
}

pollEl.setAttribute( 'data-pd-init-done', '1' );
const settings = pollEl.getAttribute( 'data-settings' );
const poll = settings ? JSON.parse( settings ) : null;
const poll = parseJSON( pollEl.getAttribute( 'data-settings' ) );

if ( poll ) {
let poll_url;
try {
poll_url = new URL( poll.url, 'https://invalid.tld' );
poll_url = new w.URL( poll.url, 'https://invalid.tld' );
} catch ( error ) {
return false;
}
Expand All @@ -43,16 +48,13 @@
let scriptContents = '';

ratings.forEach( ratingEl => {
const isInitialized = ratingEl.getAttribute( 'data-pd-init-done' );

if ( isInitialized ) {
if ( ratingEl.hasAttribute( 'data-pd-init-done' ) ) {
return;
}

ratingEl.setAttribute( 'data-pd-init-done', '1' );

const settings = ratingEl.getAttribute( 'data-settings' );
const rating = settings ? JSON.parse( settings ) : null;
const rating = parseJSON( ratingEl.getAttribute( 'data-settings' ) );

if ( rating ) {
scriptContents += `
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: enhancement
Comment: Review suggestions that came in after the PR was merged.


0 comments on commit cb3eb70

Please sign in to comment.