| 1 |
(function( $ ) { |
| 2 |
'use strict'; |
| 3 |
|
| 4 |
/** |
| 5 |
* Display the media uploader. |
| 6 |
* |
| 7 |
* @since 1.0.0 |
| 8 |
*/ |
| 9 |
function ayg_render_media_uploader( $elem ) { |
| 10 |
var file_frame, attachment; |
| 11 |
|
| 12 |
// If an instance of file_frame already exists, then we can open it rather than creating a new instance |
| 13 |
if ( file_frame ) { |
| 14 |
file_frame.open(); |
| 15 |
return; |
| 16 |
}; |
| 17 |
|
| 18 |
// Use the wp.media library to define the settings of the media uploader |
| 19 |
file_frame = wp.media.frames.file_frame = wp.media({ |
| 20 |
frame: 'post', |
| 21 |
state: 'insert', |
| 22 |
multiple: false |
| 23 |
}); |
| 24 |
|
| 25 |
// Setup an event handler for what to do when a media has been selected |
| 26 |
file_frame.on( 'insert', function() { |
| 27 |
// Read the JSON data returned from the media uploader |
| 28 |
attachment = file_frame.state().get( 'selection' ).first().toJSON(); |
| 29 |
|
| 30 |
// First, make sure that we have the URL of the media to display |
| 31 |
if ( 0 > $.trim( attachment.url.length ) ) { |
| 32 |
return; |
| 33 |
}; |
| 34 |
|
| 35 |
// Set the data |
| 36 |
$elem.prev( '.ayg-settings-url' ) |
| 37 |
.val( attachment.url ); |
| 38 |
}); |
| 39 |
|
| 40 |
// Now display the actual file_frame |
| 41 |
file_frame.open(); |
| 42 |
}; |
| 43 |
|
| 44 |
/** |
| 45 |
* Get Youtube playlist ID from Youtube URL. |
| 46 |
* |
| 47 |
* @since 1.0.0 |
| 48 |
* @param {string} url - URL from which to extract the ID. |
| 49 |
* @return {string} |
| 50 |
*/ |
| 51 |
function ayg_get_youtube_playlist_id( url ) { |
| 52 |
var id = /[&|\?]list=([a-zA-Z0-9_-]+)/gi.exec( url ); |
| 53 |
return ( id && id.length > 0 ) ? id[1] : url; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Get Youtube channel ID from Youtube URL. |
| 58 |
* |
| 59 |
* @since 1.0.0 |
| 60 |
* @param {string} url - URL from which to extract the ID. |
| 61 |
* @return {object} |
| 62 |
*/ |
| 63 |
function ayg_get_youtube_channel_id( url ) { |
| 64 |
var type = 'channel'; |
| 65 |
var id = url; |
| 66 |
|
| 67 |
url = url.replace( /(>|<)/gi, '' ).split( /(\/channel\/|\/user\/)/ ); |
| 68 |
|
| 69 |
if ( url[2] !== undefined ) { |
| 70 |
id = url[2].split( /[^0-9a-z_-]/i ); |
| 71 |
id = id[0]; |
| 72 |
} |
| 73 |
|
| 74 |
if ( /\/user\//.test( url ) ) { |
| 75 |
type = 'username'; |
| 76 |
} |
| 77 |
|
| 78 |
return { |
| 79 |
type: type, |
| 80 |
id: id |
| 81 |
}; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Get Youtube video ID from Youtube URL. |
| 86 |
* |
| 87 |
* @since 1.0.0 |
| 88 |
* @param {string} url - URL from which to extract the ID. |
| 89 |
* @return {string} |
| 90 |
*/ |
| 91 |
function ayg_get_youtube_video_id( url ) { |
| 92 |
var id = url; |
| 93 |
|
| 94 |
url = url.replace( /(>|<)/gi, '' ).split( /(vi\/|v=|\/v\/|youtu\.be\/|\/embed\/)/ ); |
| 95 |
|
| 96 |
if ( url[2] !== undefined ) { |
| 97 |
id = url[2].split( /[^0-9a-z_\-]/i ); |
| 98 |
id = id[0]; |
| 99 |
} |
| 100 |
|
| 101 |
return id; |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Widget: Initiate color picker |
| 106 |
* |
| 107 |
* @since 1.0.0 |
| 108 |
*/ |
| 109 |
function ayg_widget_color_picker( widget ) { |
| 110 |
widget.find( '.ayg-color-picker' ).wpColorPicker( { |
| 111 |
change: _.throttle( function() { // For Customizer |
| 112 |
$( this ).trigger( 'change' ); |
| 113 |
}, 3000 ) |
| 114 |
}); |
| 115 |
} |
| 116 |
|
| 117 |
function on_ayg_widget_update( event, widget ) { |
| 118 |
ayg_widget_color_picker( widget ); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Called when the page has loaded. |
| 123 |
* |
| 124 |
* @since 1.0.0 |
| 125 |
*/ |
| 126 |
$(function() { |
| 127 |
// Common: Initialize the color picker |
| 128 |
$( '.ayg-color-picker' ).wpColorPicker(); |
| 129 |
|
| 130 |
// Common: Init the popup. |
| 131 |
if ( $.fn.magnificPopup ) { |
| 132 |
$( '.ayg-modal-button' ).magnificPopup({ |
| 133 |
type: 'inline', |
| 134 |
mainClass: 'mfp-fade' |
| 135 |
}); |
| 136 |
} |
| 137 |
|
| 138 |
// Dashboard: Save API Key |
| 139 |
$( '#ayg-button-save-api-key' ).on( 'click', function( e ) { |
| 140 |
e.preventDefault(); |
| 141 |
|
| 142 |
$( this ).prop( 'disabled', true ); |
| 143 |
$( '.ayg-ajax-status', '#ayg-table-api-key' ).html( '<span class="spinner"></span>' ); |
| 144 |
|
| 145 |
var data = { |
| 146 |
'action': 'ayg_save_api_key', |
| 147 |
'api_key': $( '#ayg-api-key' ).val(), |
| 148 |
'security': ayg_admin.ajax_nonce |
| 149 |
}; |
| 150 |
|
| 151 |
if ( ! data.api_key ) { |
| 152 |
$( this ).prop( 'disabled', false ); |
| 153 |
$( '.ayg-ajax-status', '#ayg-table-api-key' ).html( '<span class="ayg-text-error">' + ayg_admin.i18n.invalid_api_key + '</span>' ); |
| 154 |
|
| 155 |
return false; |
| 156 |
} |
| 157 |
|
| 158 |
$.post( ajaxurl, data, function( response ) { |
| 159 |
window.location.reload(); // Reload document |
| 160 |
}); |
| 161 |
}); |
| 162 |
|
| 163 |
// Dashboard: Generate shortcode |
| 164 |
$( '#ayg-generate-shortcode' ).on( 'click', function( e ) { |
| 165 |
e.preventDefault(); |
| 166 |
|
| 167 |
// Attributes |
| 168 |
var props = {}; |
| 169 |
var popup = 0; |
| 170 |
|
| 171 |
// Loop through all editor fields |
| 172 |
$( '.ayg-editor-control', '#ayg-shortcode-builder' ).each(function() { |
| 173 |
var $elem = $( this ).find( '.ayg-editor-field' ); |
| 174 |
var type = $elem.attr( 'type' ); |
| 175 |
var key = $elem.attr( 'name' ); |
| 176 |
var value = $elem.val(); |
| 177 |
var def = $elem.data( 'default' ); |
| 178 |
|
| 179 |
// Skip popup field |
| 180 |
if ( 'popup' == key ) { |
| 181 |
popup = $elem.is( ':checked' ) ? 1 : 0; |
| 182 |
return true; // continue |
| 183 |
} |
| 184 |
|
| 185 |
// field type = checkbox |
| 186 |
if ( 'checkbox' == type ) { |
| 187 |
value = $elem.is( ':checked' ) ? 1 : 0; |
| 188 |
} |
| 189 |
|
| 190 |
// source = playlist |
| 191 |
if ( 'playlist' == key ) { |
| 192 |
value = ayg_get_youtube_playlist_id( value ); |
| 193 |
} |
| 194 |
|
| 195 |
// source = channel |
| 196 |
if ( 'channel' == key ) { |
| 197 |
var result = ayg_get_youtube_channel_id( value ); |
| 198 |
|
| 199 |
key = result.type; |
| 200 |
value = result.id; |
| 201 |
|
| 202 |
if ( props.hasOwnProperty( 'type' ) && 'channel' == props.type ) { |
| 203 |
props.type = key; |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
// source = video |
| 208 |
if ( 'video' == key ) { |
| 209 |
value = ayg_get_youtube_video_id( value ); |
| 210 |
} |
| 211 |
|
| 212 |
// source = videos |
| 213 |
if ( 'videos' == key ) { |
| 214 |
var lines = value.split( '\n' ), |
| 215 |
ids = []; |
| 216 |
|
| 217 |
lines.map(function( url ) { |
| 218 |
ids.push( ayg_get_youtube_video_id( url ) ); |
| 219 |
}); |
| 220 |
|
| 221 |
value = ids.join( ',' ); |
| 222 |
} |
| 223 |
|
| 224 |
// Add only if the user input differ from the global configuration |
| 225 |
if ( value != def ) { |
| 226 |
props[ key ] = value; |
| 227 |
} |
| 228 |
}); |
| 229 |
|
| 230 |
// If popup is enabled and type is video, force theme to popup |
| 231 |
if ( popup && props.hasOwnProperty( 'type' ) && 'video' == props.type ) { |
| 232 |
props.theme = 'popup'; |
| 233 |
} |
| 234 |
|
| 235 |
// Build attributes string |
| 236 |
var attrs = ''; |
| 237 |
|
| 238 |
for ( var key in props ) { |
| 239 |
if ( props.hasOwnProperty( key ) ) { |
| 240 |
attrs += ( ' ' + key + '="' + props[ key ] + '"' ); |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
// Shortcode output |
| 245 |
$( '#aiovg-shortcode').val( '[automatic_youtube_gallery' + attrs + ']' ); |
| 246 |
}); |
| 247 |
|
| 248 |
// Editor: Toggle between field sections |
| 249 |
$( document ).on( 'click', '.ayg-editor-section-header', function( e ) { |
| 250 |
var $elem = $( this ).parent(); |
| 251 |
|
| 252 |
if ( ! $elem.hasClass( 'ayg-active' ) ) { |
| 253 |
$( this ).closest( '.ayg-editor' ) |
| 254 |
.find( '.ayg-editor-section.ayg-active' ) |
| 255 |
.toggleClass( 'ayg-active' ) |
| 256 |
.find( '.ayg-editor-controls' ) |
| 257 |
.slideToggle(); |
| 258 |
} |
| 259 |
|
| 260 |
$elem.toggleClass( 'ayg-active' ) |
| 261 |
.find( '.ayg-editor-controls' ) |
| 262 |
.slideToggle(); |
| 263 |
}); |
| 264 |
|
| 265 |
// Editor: Toggle fields based on the source type |
| 266 |
$( document ).on( 'change', '.ayg-editor-field-type', function() { |
| 267 |
var $container = $( this ).closest( '.ayg-editor' ); |
| 268 |
var value = $( this ).val(); |
| 269 |
|
| 270 |
$container.removeClass(function( index, classes ) { |
| 271 |
var matches = classes.match( /\ayg-editor-field-type-\S+/ig ); |
| 272 |
return ( matches ) ? matches.join( ' ' ) : ''; |
| 273 |
}); |
| 274 |
|
| 275 |
$container.addClass( 'ayg-editor-field-type-' + value ); |
| 276 |
}); |
| 277 |
|
| 278 |
// Editor: Toggle fields based on the theme |
| 279 |
$( document ).on( 'change', '.ayg-editor-field-theme', function() { |
| 280 |
var $container = $( this ).closest( '.ayg-editor' ); |
| 281 |
var value = $( this ).val(); |
| 282 |
|
| 283 |
$container.removeClass(function( index, classes ) { |
| 284 |
var matches = classes.match( /\ayg-editor-field-theme-\S+/ig ); |
| 285 |
return ( matches ) ? matches.join( ' ' ) : ''; |
| 286 |
}); |
| 287 |
|
| 288 |
$container.addClass( 'ayg-editor-field-theme-' + value ); |
| 289 |
}); |
| 290 |
|
| 291 |
// Settings: Toggle fields based on the theme |
| 292 |
$( 'tr.theme select', '#ayg-settings' ).on( 'change', function() { |
| 293 |
var $container = $( '#ayg-settings' ); |
| 294 |
var value = $( this ).val(); |
| 295 |
|
| 296 |
$container.removeClass(function( index, classes ) { |
| 297 |
var matches = classes.match( /theme-\S+/ig ); |
| 298 |
return ( matches ) ? matches.join( ' ' ) : ''; |
| 299 |
}); |
| 300 |
|
| 301 |
$container.addClass( 'theme-' + value ); |
| 302 |
}); |
| 303 |
|
| 304 |
// Settings: Toggle fields based on the player type |
| 305 |
$( 'tr.player_type input[type="radio"]', '#ayg-settings' ).on( 'change', function() { |
| 306 |
var $container = $( '#ayg-settings' ); |
| 307 |
var value = $container.find( 'tr.player_type input[type="radio"]:checked' ).val(); |
| 308 |
|
| 309 |
$container.removeClass(function( index, classes ) { |
| 310 |
var matches = classes.match( /player_type-\S+/ig ); |
| 311 |
return ( matches ) ? matches.join( ' ' ) : ''; |
| 312 |
}); |
| 313 |
|
| 314 |
$container.addClass( 'player_type-' + value ); |
| 315 |
}); |
| 316 |
|
| 317 |
// Settings: Browse button |
| 318 |
$( '.ayg-settings-browse' ).on( 'click', function( e ) { |
| 319 |
e.preventDefault(); |
| 320 |
ayg_render_media_uploader( $( this ) ); |
| 321 |
}); |
| 322 |
|
| 323 |
// Settings: Delete cache |
| 324 |
$( '#ayg-button-delete-cache' ).on( 'click', function( e ) { |
| 325 |
e.preventDefault(); |
| 326 |
|
| 327 |
$( this ).prop( 'disabled', true ); |
| 328 |
$( '.ayg-ajax-status', '#ayg-table-delete-cache' ).html( '<span class="spinner"></span>' ); |
| 329 |
|
| 330 |
var data = { |
| 331 |
'action': 'ayg_delete_cache', |
| 332 |
'security': ayg_admin.ajax_nonce |
| 333 |
}; |
| 334 |
|
| 335 |
$.post( ajaxurl, data, function( response ) { |
| 336 |
$( this ).prop( 'disabled', false ); |
| 337 |
$( '.ayg-ajax-status', '#ayg-table-delete-cache' ).html( '<span class="ayg-text-success">' + ayg_admin.i18n.cleared + '</span>' ); |
| 338 |
}); |
| 339 |
}); |
| 340 |
|
| 341 |
// Widget: Initiate color picker |
| 342 |
$( '#widgets-right .widget:has(.ayg-color-picker)' ).each(function() { |
| 343 |
ayg_widget_color_picker( $( this ) ); |
| 344 |
}); |
| 345 |
|
| 346 |
$( document ).on( 'widget-added widget-updated', on_ayg_widget_update ); |
| 347 |
}); |
| 348 |
|
| 349 |
})( jQuery ); |
| 350 |
|