yoast-premium-social-preview.js
24.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
/* global yoastSocialPreview, tinyMCE, require, wp, YoastSEO, ajaxurl */
/* jshint -W097 */
var getImages = require( "yoastseo/js/stringProcessing/imageInText" );
var helpPanel = require( "./helpPanel" );
var getTitlePlaceholder = require( "../../../../js/src/analysis/getTitlePlaceholder" );
var getDescriptionPlaceholder = require( "../../../../js/src/analysis/getDescriptionPlaceholder" );
var _debounce = require("lodash/debounce");
var clone = require( "lodash/clone" );
var forEach = require( "lodash/forEach" );
var _has = require( "lodash/has" );
var isUndefined = require( "lodash/isUndefined" );
var Jed = require( "jed" );
var socialPreviews = require( "yoast-social-previews" );
( function( $ ) {
/**
* We want to store the fallbacks in an object, to have directly access to them.
* @type {{content: string, featured: string}}
*/
var imageFallBack = {
content: "",
featured: "",
};
var canReadFeaturedImage = true;
var FacebookPreview = socialPreviews.FacebookPreview;
var TwitterPreview = socialPreviews.TwitterPreview;
var facebookPreview, twitterPreview;
var translations = yoastSocialPreview.i18n;
var i18n = new Jed( addLibraryTranslations( translations.library ) );
var biggerImages = {};
let postTitleInputId = "title";
/**
* Sets the events for opening the WP media library when pressing the button.
*
* @param {Object} imageUrl The image URL object.
* @param {string} imageButton ID name for the image button.
* @param {string} removeButton ID name for the remove button.
* @param {function} onMediaSelect The event that will be ran when image is chosen.
* @param {Object} imagePreviewElement The image preview element that can be clicked to update as well.
*
* @returns {void}
*/
function bindUploadButtonEvents( imageUrl, imageButton, removeButton, onMediaSelect, imagePreviewElement ) {
var socialPreviewUploader = wp.media.frames.file_frame = wp.media( {
title: yoastSocialPreview.choose_image,
button: { text: yoastSocialPreview.choose_image },
multiple: false,
} );
socialPreviewUploader.on( "select", function() {
var attachment = socialPreviewUploader.state().get( "selection" ).first().toJSON();
// Set the image URL.
imageUrl.val( attachment.url );
onMediaSelect();
$( removeButton ).show();
} );
$( removeButton ).click( function( evt ) {
evt.preventDefault();
// Clear the image URL
imageUrl.val( "" );
onMediaSelect();
$( removeButton ).hide();
} );
$( imageButton ).click( function( evt ) {
evt.preventDefault();
socialPreviewUploader.open();
} );
$( imagePreviewElement ).on( "click", function( eventObject ) {
socialPreviewUploader.open();
} );
}
/**
* Adds the choose image button and hides the input field.
*
* @param {Object} preview The preview to add the upload button to.
*
* @returns {void}
*/
function addUploadButton( preview ) {
if ( typeof wp.media === "undefined" ) {
return;
}
var imageUrl = $( preview.element.formContainer ).find( ".js-snippet-editor-imageUrl" );
var buttonDiv = $( "<div></div>" );
buttonDiv.insertAfter( imageUrl );
var uploadButtonText = getUploadButtonText( preview );
var imageFieldId = jQuery( imageUrl ).attr( "id" );
var imageButtonId = imageFieldId + "_button";
var imageButtonHtml = '<button id="' + imageButtonId + '" ' +
'class="button button-primary wpseo_preview_image_upload_button" type="button">' + uploadButtonText + '</button>';
var removeButtonId = imageFieldId + "_remove_button";
var removeButtonHtml = '<button id="' + removeButtonId + '" type="button" ' +
'class="button wpseo_preview_image_upload_button">' + yoastSocialPreview.removeImageButton + '</button>';
$( buttonDiv ).append( imageButtonHtml );
$( buttonDiv ).append( removeButtonHtml );
imageUrl.hide();
if ( imageUrl.val() === "" ) {
$( "#" + removeButtonId ).hide();
}
bindUploadButtonEvents(
imageUrl,
"#" + imageButtonId,
"#" + removeButtonId,
preview.updatePreview.bind( preview ),
$( preview.element.container ).find( ".editable-preview__image" )
);
}
/**
* Returns the type of the current page: post or term.
*
* @returns {string} The current type.
*/
function getCurrentType() {
// When this field exists, it is a post.
if ( $( "#post_ID" ).length > 0 ) {
return "post";
}
// When this field is found, it is a term.
if ( $( "input[name=tag_ID]" ).length > 0 ) {
return "term";
}
return "";
}
/**
* Returns the prefix for the fields, because of the fields for the post do have an othere prefix than the ones for
* a taxonomy.
*
* @returns {*} The prefix to use.
*/
function fieldPrefix() {
switch( getCurrentType() ) {
case "post":
return "yoast_wpseo";
case "term":
return "wpseo";
default :
return "";
}
}
/**
* Returns the name of the tinymce and textarea fields.
*
* @returns {string} The name for the content field.
*/
function contentTextName() {
switch ( getCurrentType() ) {
case "post" :
return "content";
case "term" :
return "description";
default :
return "";
}
}
/**
* Creates the social preview container and hides the old form table, to replace it.
*
* @param {Object} socialPreviewholder The holder element where the container will be append to.
* @param {string} containerId The id the container will get
* @returns {void}
*/
function createSocialPreviewContainer( socialPreviewholder, containerId ) {
socialPreviewholder.append( '<div id="' + containerId + '"></div>' );
socialPreviewholder.find( ".form-table" ).hide();
}
/**
* Gets the meta description from the snippet editor
* @returns {void}
*/
function getMetaDescription() {
return $( "#yoast_wpseo_metadesc" ).val();
}
/**
* Returns the placeholder for the meta description field.
*
* @returns {string} The placeholder for the meta description.
*/
function getSocialDescriptionPlaceholder() {
var description = getMetaDescription();
if ( "" === description ) {
description = getDescriptionPlaceholder();
}
return description;
}
/**
* Returns the arguments for the social preview prototypes.
*
* @param {string} targetElement The element where the preview is loaded.
* @param {string} fieldPrefix The prefix each form element has.
*
* @returns { {
* targetElement: Element,
* data: {title: *, description: *, imageUrl: *},
* baseURL: *,
* callbacks: {updateSocialPreview: callbacks.updateSocialPreview}
* } } The arguments for the social preview.
*/
function getSocialPreviewArgs( targetElement, fieldPrefix ) {
var titlePlaceholder = getTitlePlaceholder();
var descriptionPlaceholder = getSocialDescriptionPlaceholder();
var args = {
targetElement: $( targetElement ).get( 0 ),
data: {
title: $( "#" + fieldPrefix + "-title" ).val(),
description: $( "#" + fieldPrefix + "-description" ).val(),
imageUrl: $( "#" + fieldPrefix + "-image" ).val(),
},
baseURL: yoastSocialPreview.website,
callbacks: {
updateSocialPreview: function( data ) {
$( "#" + fieldPrefix + "-title" ).val( data.title );
$( "#" + fieldPrefix + "-description" ).val( data.description );
$( "#" + fieldPrefix + "-image" ).val( data.imageUrl );
// Make sure Twitter is updated if a Facebook image is set
$( ".editable-preview" ).trigger( "imageUpdate" );
if ( data.imageUrl !== "" ) {
var buttonPrefix = targetElement.attr( "id" ).replace( "Preview", "" );
setUploadButtonValue( buttonPrefix, yoastSocialPreview.useOtherImage );
}
jQuery( targetElement ).find( ".editable-preview" ).trigger( "titleUpdate" );
jQuery( targetElement ).find( ".editable-preview" ).trigger( "descriptionUpdate" );
},
modifyImageUrl: function( imageUrl ) {
if ( imageUrl === "" ) {
imageUrl = getFallbackImage( "" );
}
return imageUrl;
},
modifyTitle: function( title ) {
if ( fieldPrefix.indexOf( "twitter" ) > -1 ) {
if ( title === $( "#twitter-editor-title" ).attr( "placeholder" ) ) {
var facebookTitle = $( "#facebook-editor-title" ).val();
if ( ! isUndefined( facebookTitle ) && facebookTitle !== "" ) {
title = facebookTitle;
}
}
}
return YoastSEO.wp.replaceVarsPlugin.replaceVariables( title );
},
modifyDescription: function( description ) {
if ( fieldPrefix.indexOf( "twitter" ) > -1 ) {
if ( description === $( "#twitter-editor-description" ).attr( "placeholder" ) ) {
var facebookDescription = $( "#facebook-editor-description" ).val();
if ( facebookDescription !== "" ) {
description = facebookDescription;
}
}
if ( isUndefined( description ) ){
description = $( '#twitter-editor-description' ).attr( 'placeholder' );
}
}
return YoastSEO.wp.replaceVarsPlugin.replaceVariables( description );
},
},
placeholder: {
title: titlePlaceholder,
},
defaultValue: {
title: titlePlaceholder,
},
};
if ( "" !== descriptionPlaceholder ) {
args.placeholder.description = descriptionPlaceholder;
args.defaultValue.description = descriptionPlaceholder;
}
return args;
}
/**
* Try to get the Facebook author name via AJAX and put it to the Facebook preview.
*
* @param {FacebookPreview} facebookPreview The Facebook preview object
* @returns {void}
*/
function getFacebookAuthor( facebookPreview ) {
$.get(
ajaxurl,
{
action: "wpseo_get_facebook_name",
_ajax_nonce: yoastSocialPreview.facebookNonce,
user_id: $( "#post_author_override" ).val(),
},
function( author ) {
if ( author !== 0 ) {
facebookPreview.setAuthor( author );
}
}
);
}
/**
* Initialize the Facebook preview.
*
* @param {Object} facebookHolder Target element for adding the Facebook preview.
* @returns {void}
*/
function initFacebook( facebookHolder ) {
createSocialPreviewContainer( facebookHolder, "facebookPreview" );
var facebookPreviewContainer = $( "#facebookPreview" );
facebookPreview = new FacebookPreview(
getSocialPreviewArgs( facebookPreviewContainer, fieldPrefix() + "_opengraph" ),
i18n
);
facebookPreviewContainer.on(
"imageUpdate",
".editable-preview",
function() {
setUploadButtonValue( "facebook", getUploadButtonText( facebookPreview ) );
setFallbackImage( facebookPreview );
}
);
facebookPreview.init();
addUploadButton( facebookPreview );
var postAuthorDropdown = $( "#post_author_override" );
if( postAuthorDropdown.length > 0 ) {
postAuthorDropdown.on( "change", getFacebookAuthor.bind( this, facebookPreview ) );
postAuthorDropdown.trigger( "change" );
}
$( "#" + postTitleInputId ).on(
"keydown keyup input focus blur",
_debounce( facebookPreview.updatePreview.bind( facebookPreview ), 500 )
);
}
/**
* Initialize the twitter preview.
*
* @param {Object} twitterHolder Target element for adding the twitter preview.
* @returns {void}
*/
function initTwitter( twitterHolder ) {
createSocialPreviewContainer( twitterHolder, "twitterPreview" );
var twitterPreviewContainer = $( "#twitterPreview" );
twitterPreview = new TwitterPreview(
getSocialPreviewArgs( twitterPreviewContainer, fieldPrefix() + "_twitter" ),
i18n
);
twitterPreviewContainer.on(
"imageUpdate",
".editable-preview",
function() {
setUploadButtonValue( "twitter", getUploadButtonText( twitterPreview ) );
setFallbackImage( twitterPreview );
}
);
var facebookPreviewContainer = $( "#facebookPreview" );
facebookPreviewContainer.on(
"titleUpdate",
".editable-preview",
twitterTitleFallback.bind( this, twitterPreview )
);
facebookPreviewContainer.on(
"descriptionUpdate",
".editable-preview",
twitterDescriptionFallback.bind( this, twitterPreview )
);
twitterPreview.init();
addUploadButton( twitterPreview );
twitterTitleFallback( twitterPreview );
twitterDescriptionFallback( twitterPreview );
$( "#" + postTitleInputId ).on(
"keydown keyup input focus blur",
_debounce( twitterTitleFallback.bind( this, twitterPreview ), 500 )
);
}
/**
* When twitter title is empty, use the Facebook title
*
* @param {TwitterPreview} twitterPreview The twitter preview object
* @returns {void}
*/
function twitterTitleFallback( twitterPreview ) {
var $twitterTitle = $( "#twitter-editor-title" );
var twitterTitle = $twitterTitle.val();
if( twitterTitle !== "" ) {
return;
}
var facebookTitle = $( "#facebook-editor-title" ).val();
if ( ! isUndefined( facebookTitle ) && facebookTitle !== "" ) {
twitterPreview.setTitle( facebookTitle );
} else {
twitterPreview.setTitle( $twitterTitle.attr( "placeholder" ) );
}
}
/**
* When twitter description is empty, use the description title
*
* @param {TwitterPreview} twitterPreview The twitter preview object
* @returns {void}
*/
function twitterDescriptionFallback( twitterPreview ) {
var $twitterDescription = $( "#twitter-editor-description" );
var twitterDescription = $twitterDescription.val();
if( twitterDescription !== "" ) {
return;
}
var facebookDescription = $( "#facebook-editor-description" ).val();
if ( facebookDescription !== "" ) {
twitterPreview.setDescription( facebookDescription );
} else {
twitterPreview.setDescription( $twitterDescription.attr( "placeholder" ) );
}
}
/**
* Set the fallback image for the preview if no image has been set
*
* @param {Object} preview Preview to set fallback image on.
* @returns {void}
*/
function setFallbackImage( preview ) {
if ( preview.data.imageUrl === "" ) {
preview.setImage( getFallbackImage( "" ) );
}
}
/**
* Changes the upload button value when there are fallback images present.
*
* @param {string} buttonPrefix The value before the id name.
* @param {string} text The text on the button.
* @returns {void}
*/
function setUploadButtonValue( buttonPrefix, text ) {
$( "#" + buttonPrefix + "-editor-imageUrl_button" ).html( text );
}
/**
* Bind the image events to set the fallback and rendering the preview.
*
* @returns {void}
*/
function bindImageEvents() {
if ( getCurrentType() === "post" ) {
bindFeaturedImageEvents();
}
bindContentEvents();
}
/**
* Get the text that the upload button needs to display
*
* @param {Object} preview Preview to read image from.
* @returns {*} The text for the button.
*/
function getUploadButtonText( preview ) {
return preview.data.imageUrl === "" ? yoastSocialPreview.uploadImage : yoastSocialPreview.useOtherImage;
}
/**
* Binds the events for the featured image.
*
* @returns {void}
*/
function bindFeaturedImageEvents() {
if ( isUndefined( wp.media ) || isUndefined( wp.media.featuredImage ) ) {
return;
}
// When the featured image is being changed
var featuredImage = wp.media.featuredImage.frame();
featuredImage.on( "select", function() {
var imageDetails = featuredImage.state().get( "selection" ).first().attributes;
canReadFeaturedImage = true;
setFeaturedImage( imageDetails.url );
} );
$( "#postimagediv" ).on( "click", "#remove-post-thumbnail", function() {
canReadFeaturedImage = false;
clearFeaturedImage();
} );
}
/**
* Bind the events for the content.
*
* @returns {void}
*/
function bindContentEvents() {
// Bind the event when something changed in the text editor.
var contentElement = $( "#" + contentTextName() );
if ( contentElement.length > 0 ) {
contentElement.on( "input", detectImageFallback );
}
// Bind the events when something changed in the tinyMCE editor.
if ( typeof tinyMCE !== "undefined" && typeof tinyMCE.on === "function" ) {
var events = [ "input", "change", "cut", "paste" ];
tinyMCE.on( "addEditor", function( e ) {
for ( var i = 0; i < events.length; i++ ) {
e.editor.on( events[ i ], detectImageFallback );
}
} );
}
}
/**
* Sets the featured image fallback value as an empty value and runs the fallback method.
*
* @returns {void}
*/
function clearFeaturedImage() {
setFeaturedImage( "" );
detectImageFallback();
}
/**
* Sets the image fallbacks like the featured image (in case of a post) and the content image.
*
* @returns {void}
*/
function detectImageFallback() {
// In case of a post: we want to have the featured image.
if ( getCurrentType() === "post" ) {
var featuredImage = getFeaturedImage();
setFeaturedImage( featuredImage );
if ( featuredImage !== "" ) {
return;
}
}
setContentImage( getContentImage( function( image ) {
setContentImage( image );
} ) );
}
/**
* Sets the featured image based on the given image URL.
*
* @param {string} featuredImage The image we want to set.
* @returns {void}
*/
function setFeaturedImage( featuredImage ) {
if ( imageFallBack.featured !== featuredImage ) {
imageFallBack.featured = featuredImage;
// Just refresh the image URL.
$( ".editable-preview" ).trigger( "imageUpdate" );
}
}
/**
* Sets the content image base on the given image URL
*
* @param {string} contentImage The image we want to set.
* @returns {void}
*/
function setContentImage( contentImage ) {
if ( imageFallBack.content !== contentImage ) {
imageFallBack.content = contentImage;
// Just refresh the image URL.
$( ".editable-preview" ).trigger( "imageUpdate" );
}
}
/**
* Gets the featured image source from the DOM.
*
* @returns {string} The url to the featured image.
*/
function getFeaturedImage() {
if ( canReadFeaturedImage === false ) {
return "";
}
var postThumbnail = $( ".attachment-post-thumbnail" );
if ( postThumbnail.length > 0 ) {
return $( postThumbnail.get( 0 ) ).attr( "src" );
}
return "";
}
/**
* Returns the image from the content.
*
* @param {Function} callback function to call if a bigger size is available.
*
* @returns {string} The first image found in the content.
*/
function getContentImage( callback ) {
var content = getContent();
var images = getImages( content );
var image = "";
if ( images.length === 0 ) {
return image;
}
do {
var currentImage = images.shift();
currentImage = $( currentImage );
var imageSource = currentImage.prop( "src" );
if ( imageSource ) {
image = imageSource;
}
} while ( "" === image && images.length > 0 );
image = getBiggerImage( image, callback );
return image;
}
/**
* Try to retrieve a bigger image for a certain image found in the content.
*
* @param {string} url The URL to retrieve.
* @param {Function} callback The callback to call if there is a bigger image.
* @returns {string} Returns the bigger image url.
*/
function getBiggerImage( url, callback ) {
if ( _has( biggerImages, url ) ) {
return biggerImages[ url ];
}
retrieveImageDataFromURL( url, function( imageUrl ) {
biggerImages[ url ] = imageUrl;
callback( imageUrl );
} );
return url;
}
/**
* Retrieves the image metadata from an image url and saves it to the image manager afterwards
*
* @param {string} url The image URL to retrieve the metadata from.
* @param {Function} callback Callback to call with the image URL result.
* @returns {void}
*/
function retrieveImageDataFromURL( url, callback ) {
$.getJSON( ajaxurl, {
action: "retrieve_image_data_from_url",
imageURL: url,
}, function( response ) {
if ( "success" === response.status ) {
callback( response.result );
}
} );
}
/**
* Returns the content from current visible content editor
*
* @returns {string} The value of the tinymce box.
*/
function getContent() {
if ( isTinyMCEAvailable() ) {
return tinyMCE.get( contentTextName() ).getContent();
}
var contentElement = $( "#" + contentTextName() );
if ( contentElement.length > 0 ) {
return contentElement.val();
}
return "";
}
/**
* Check if tinymce is active on the current page.
*
* @returns {boolean} True when tinymce is available.
* @private
*/
function isTinyMCEAvailable() {
if ( typeof tinyMCE === "undefined" ||
typeof tinyMCE.editors === "undefined" ||
tinyMCE.editors.length === 0 ||
tinyMCE.get( contentTextName() ) === null ||
tinyMCE.get( contentTextName() ).isHidden() ) {
return false;
}
return true;
}
/**
* Check if there is a fallback image like the featured image or the first image in the content.
*
* @param {string} defaultImage The default image when nothing has been found.
* @returns {string} The image to use.
*/
function getFallbackImage( defaultImage ) {
// Twitter always first falls back to Facebook
if ( ! isUndefined( facebookPreview ) && facebookPreview.data.imageUrl !== "" ) {
return facebookPreview.data.imageUrl;
}
// In case of an post: we want to have the featured image.
if ( getCurrentType() === "post" ) {
if ( imageFallBack.featured !== "" ) {
return imageFallBack.featured;
}
}
// When the featured image is empty, try an image in the content
if ( imageFallBack.content !== "" ) {
return imageFallBack.content;
}
if ( defaultImage !== undefined ) {
return defaultImage;
}
return "";
}
/**
* Adds the help panels to the social previews
*
* @returns {void}
*/
function addHelpPanels() {
var panels = [
{
beforeElement: "#facebook-editor-imageUrl",
buttonText: translations.helpButton.facebookImage,
descriptionText: translations.help.facebookImage,
id: "facebook-editor-image-help",
},
{
beforeElement: "#facebook-editor-title",
buttonText: translations.helpButton.facebookTitle,
descriptionText: translations.help.facebookTitle,
id: "facebook-editor-title-help",
},
{
beforeElement: "#facebook-editor-description",
buttonText: translations.helpButton.facebookDescription,
descriptionText: translations.help.facebookDescription,
id: "facebook-editor-description-help",
},
{
beforeElement: "#twitter-editor-imageUrl",
buttonText: translations.helpButton.twitterImage,
descriptionText: translations.help.twitterImage,
id: "twitter-editor-image-help",
},
{
beforeElement: "#twitter-editor-title",
buttonText: translations.helpButton.twitterTitle,
descriptionText: translations.help.twitterTitle,
id: "twitter-editor-title-help",
},
{
beforeElement: "#twitter-editor-description",
buttonText: translations.helpButton.twitterDescription,
descriptionText: translations.help.twitterDescription,
id: "twitter-editor-description-help",
},
];
forEach( panels, function( panel ) {
$( panel.beforeElement ).before(
helpPanel.helpButton( panel.buttonText, panel.id ) +
helpPanel.helpText( panel.descriptionText, panel.id )
);
} );
$( ".snippet-editor__form" ).on( "click", ".yoast-help-button", function() {
var $button = $( this ),
helpPanel = $( "#" + $button.attr( "aria-controls" ) ),
isPanelVisible = helpPanel.is( ":visible" );
$( helpPanel ).slideToggle( 200, function() {
$button.attr( "aria-expanded", ! isPanelVisible );
} );
} );
}
/**
* Adds library translations
* @param {Object} translations The translations to use.
* @returns {Object} translations mapped to the proper domain.
*/
function addLibraryTranslations( translations ) {
if ( typeof translations !== "undefined" && typeof translations.domain !== "undefined" ) {
translations.domain = "yoast-social-previews";
translations.locale_data[ "yoast-social-previews" ] = clone( translations.locale_data[ "wordpress-seo-premium" ] );
delete( translations.locale_data[ "wordpress-seo-premium" ] );
return translations;
}
return {
domain: "yoast-social-previews",
locale_data: {
"yoast-social-previews": {
"": {},
},
},
};
}
/**
* Initialize the social previews.
*
* @returns {void}
*/
function initYoastSocialPreviews() {
var facebookHolder = $( "#wpseo_facebook" );
var twitterHolder = $( "#wpseo_twitter" );
if ( facebookHolder.length > 0 || twitterHolder.length > 0 ) {
jQuery( window ).on( "YoastSEO:ready", function() {
detectImageFallback();
if ( facebookHolder.length > 0 ) {
initFacebook( facebookHolder );
}
if ( twitterHolder.length > 0 ) {
initTwitter( twitterHolder );
}
addHelpPanels();
bindImageEvents();
} );
}
}
$( initYoastSocialPreviews );
}( jQuery ) );