admin-notifications.js
5 years ago
cff-admin-scripts.js
5 years ago
cff-blocks.js
6 years ago
cff-scripts.js
5 years ago
cff-scripts.min.js
6 years ago
jquery.matchHeight-min.js
6 years ago
cff-admin-scripts.js
1207 lines
| 1 | jQuery(document).ready(function($) { |
| 2 | |
| 3 | //Tooltips |
| 4 | jQuery('#cff-admin .cff-tooltip-link').click(function(){ |
| 5 | jQuery(this).closest('tr, h3, div').find('.cff-tooltip').slideToggle(); |
| 6 | }); |
| 7 | |
| 8 | //Toggle Access Token field |
| 9 | if( jQuery('#cff_show_access_token').is(':checked') ) jQuery('.cff-access-token-hidden').show(); |
| 10 | jQuery('#cff_show_access_token').change(function(){ |
| 11 | jQuery('.cff-access-token-hidden').fadeToggle(); |
| 12 | }); |
| 13 | |
| 14 | |
| 15 | //The "cff_ppca_access_token_invalid" transient is set if the access token doesn't match the ID specified. Use an Ajax call to check whether that transient is set, and if so, then displays a notice under the access token field. This used so we don't need to make an API call every time the page loads. It stores the value in this transient and checks it via ajax. |
| 16 | $.ajax({ |
| 17 | url : cffA.ajax_url, |
| 18 | type : 'get', |
| 19 | data : { |
| 20 | action : 'cff_ppca_token_check_flag' |
| 21 | }, |
| 22 | success : function(data) { |
| 23 | if( data ) $('.cff-ppca-check-notice.cff-error').show(); |
| 24 | }, |
| 25 | error : function(e) { |
| 26 | console.log(e); |
| 27 | } |
| 28 | }); |
| 29 | |
| 30 | //If no transient exists (eg. after the settings are saved) then check the API to see whether the ID and token match |
| 31 | if( typeof jQuery('#cff_access_token').attr('data-check-ppca') !== 'undefined' ){ |
| 32 | |
| 33 | var page_id = $('#cff-admin #cff_page_id').val(), |
| 34 | access_token = $('#cff-admin #cff_access_token').val(); |
| 35 | |
| 36 | if( page_id.indexOf(',') == -1 && access_token.indexOf(',') == -1 ){ |
| 37 | var ppca_check_url = 'https://graph.facebook.com/v8.0/'+page_id+'/posts?limit=1&access_token='+access_token; |
| 38 | |
| 39 | $.ajax({ |
| 40 | url: ppca_check_url, |
| 41 | type: 'GET', |
| 42 | dataType: "jsonp", |
| 43 | cache: false, |
| 44 | success: function(data) { |
| 45 | if (typeof data.error !== 'undefined') { |
| 46 | if( data.error.message.indexOf('Public Content') !== -1 ){ |
| 47 | //If the API response shows a PPCA error then show the notice below the access token field |
| 48 | $('.cff-ppca-check-notice.cff-error').show(); |
| 49 | |
| 50 | //Store the API response in a transient which is then checked above on line 29 |
| 51 | $.ajax({ |
| 52 | url : cffA.ajax_url, |
| 53 | type : 'post', |
| 54 | data : { |
| 55 | action : 'cff_ppca_token_set_flag' |
| 56 | }, |
| 57 | success : function(data) { |
| 58 | //Access token transient set |
| 59 | }, |
| 60 | error : function(e) { |
| 61 | console.log(e); |
| 62 | } |
| 63 | }); |
| 64 | } |
| 65 | } else { |
| 66 | //If the API response shows a good token then display a success notice instead |
| 67 | $('#cff-admin #cff_access_token').after('<div class="cff-ppca-check-notice cff-success" style="display: block;"><i class="fa fa-check-circle"></i> Valid Access Token</div>'); |
| 68 | } |
| 69 | }, |
| 70 | error: function(xhr,textStatus,e) { |
| 71 | console.log(e); |
| 72 | |
| 73 | return; |
| 74 | } |
| 75 | }); //End ajax |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | //Is this a page, group or profile? |
| 80 | var cff_page_type = jQuery('.cff-page-type select').val(), |
| 81 | $cff_page_type_options = jQuery('.cff-page-options'), |
| 82 | $cff_profile_error = jQuery('.cff-profile-error.cff-page-type'), |
| 83 | $cff_group_error = jQuery('.cff-group-error.cff-page-type'); |
| 84 | |
| 85 | //Should we show anything initially? |
| 86 | if(cff_page_type !== 'page') $cff_page_type_options.hide(); |
| 87 | if(cff_page_type == 'profile') $cff_profile_error.show(); |
| 88 | if(cff_page_type == 'group') $cff_group_error.show(); |
| 89 | |
| 90 | //When page type is changed show the relevant item |
| 91 | jQuery('.cff-page-type').change(function(){ |
| 92 | cff_page_type = jQuery('.cff-page-type select').val(); |
| 93 | |
| 94 | if( cff_page_type !== 'page' ) { |
| 95 | $cff_page_type_options.hide(); |
| 96 | if( cff_page_type == 'profile' ) { |
| 97 | $cff_profile_error.show(); |
| 98 | $cff_group_error.hide(); |
| 99 | } else if( cff_page_type == 'group' ) { |
| 100 | $cff_group_error.show(); |
| 101 | $cff_profile_error.hide(); |
| 102 | } else { |
| 103 | $cff_group_error.hide(); |
| 104 | $cff_profile_error.hide(); |
| 105 | } |
| 106 | |
| 107 | } else { |
| 108 | $cff_page_type_options.show(); |
| 109 | $cff_profile_error.hide(); |
| 110 | $cff_group_error.hide(); |
| 111 | } |
| 112 | }); |
| 113 | |
| 114 | |
| 115 | //Post limit manual setting |
| 116 | var cff_limit_setting = jQuery('#cff_limit_setting').val(), |
| 117 | cff_post_limit = jQuery('#cff_post_limit').val(), |
| 118 | $cff_limit_manual_settings = jQuery('#cff_limit_manual_settings'); |
| 119 | if( typeof cff_post_limit === 'undefined' ) cff_post_limit = ''; |
| 120 | |
| 121 | //Should we show anything initially? |
| 122 | if(cff_limit_setting == 'auto') $cff_limit_manual_settings.hide(); |
| 123 | if(cff_post_limit.length > 0){ |
| 124 | $cff_limit_manual_settings.show(); |
| 125 | jQuery('#cff_limit_setting').val('manual'); |
| 126 | } |
| 127 | |
| 128 | jQuery('#cff_limit_setting').change(function(){ |
| 129 | cff_limit_setting = jQuery('#cff_limit_setting').val(); |
| 130 | |
| 131 | if(cff_limit_setting == 'auto'){ |
| 132 | $cff_limit_manual_settings.hide(); |
| 133 | jQuery('#cff_post_limit').val(''); |
| 134 | } else { |
| 135 | $cff_limit_manual_settings.show(); |
| 136 | } |
| 137 | }); |
| 138 | |
| 139 | //Header Type Selection |
| 140 | var cff_header_type = jQuery('.cff-header-type select').val(), |
| 141 | $cff_facebook_header_options = jQuery('.cff-facebook-header'), |
| 142 | $cff_text_header_options = jQuery('.cff-text-header'); |
| 143 | |
| 144 | //Should we show anything initially? |
| 145 | if(cff_header_type !== 'visual') $cff_facebook_header_options.hide(); |
| 146 | if(cff_header_type !== 'text') $cff_text_header_options.hide(); |
| 147 | |
| 148 | //When Header type is changed show the relevant item |
| 149 | jQuery('.cff-header-type').change(function(){ |
| 150 | cff_header_type = jQuery('.cff-header-type select').val(); |
| 151 | |
| 152 | if( cff_header_type !== 'visual' ) { |
| 153 | $cff_facebook_header_options.hide(); |
| 154 | $cff_text_header_options.show(); |
| 155 | } else { |
| 156 | $cff_facebook_header_options.show(); |
| 157 | $cff_text_header_options.hide(); |
| 158 | } |
| 159 | }); |
| 160 | |
| 161 | //Header icon |
| 162 | //Icon type |
| 163 | //Check the saved icon type on page load and display it |
| 164 | jQuery('#cff-header-icon-example').removeClass().addClass('fa fa-' + jQuery('#cff-header-icon').val() ); |
| 165 | //Change the header icon when selected from the list |
| 166 | jQuery('#cff-header-icon').change(function() { |
| 167 | var $self = jQuery(this); |
| 168 | |
| 169 | jQuery('#cff-header-icon-example').removeClass().addClass('fa fa-' + $self.val() ); |
| 170 | }); |
| 171 | |
| 172 | |
| 173 | //Test Facebook API connection button |
| 174 | jQuery('#cff-api-test').click(function(e){ |
| 175 | e.preventDefault(); |
| 176 | //Show the JSON |
| 177 | jQuery('#cff-api-test-result textarea').css('display', 'block'); |
| 178 | }); |
| 179 | |
| 180 | |
| 181 | //If '__ days ago' date is selected then show 'Translate this' |
| 182 | var $cffTranslateDate = jQuery('#cff-translate-date'); |
| 183 | |
| 184 | if ( jQuery("#cff-date-formatting option:selected").val() == '1' ) $cffTranslateDate.show(); |
| 185 | |
| 186 | jQuery("#cff-date-formatting").change(function() { |
| 187 | if ( jQuery("#cff-date-formatting option:selected").val() == '1' ) { |
| 188 | $cffTranslateDate.fadeIn(); |
| 189 | } else { |
| 190 | $cffTranslateDate.fadeOut(); |
| 191 | } |
| 192 | }); |
| 193 | |
| 194 | //Selecting a post style |
| 195 | jQuery('.cff-post-style').click(function(){ |
| 196 | var $self = jQuery(this); |
| 197 | $('.cff_post_style').trigger('change'); |
| 198 | $self.addClass('cff-layout-selected').find('#cff_post_style').attr('checked', 'checked'); |
| 199 | $self.siblings().removeClass('cff-layout-selected'); |
| 200 | }); |
| 201 | function cffChangePostStyleSettings() { |
| 202 | setTimeout(function(){ |
| 203 | jQuery('.cff-post-style-settings').hide(); |
| 204 | jQuery('.cff-post-style-settings.cff-'+jQuery('.cff_post_style:checked').val()).show(); |
| 205 | }, 1); |
| 206 | } |
| 207 | cffChangePostStyleSettings(); |
| 208 | jQuery('.cff_post_style').change(cffChangePostStyleSettings); |
| 209 | |
| 210 | //Add the color picker |
| 211 | if( jQuery('.cff-colorpicker').length > 0 ) jQuery('.cff-colorpicker').wpColorPicker(); |
| 212 | |
| 213 | |
| 214 | //Mobile width |
| 215 | var cff_feed_width = jQuery('#cff-admin #cff_feed_width').val(), |
| 216 | $cff_width_options = jQuery('#cff-admin #cff_width_options'); |
| 217 | |
| 218 | if (typeof cff_feed_width !== 'undefined') { |
| 219 | //Show initially if a width is set |
| 220 | if(cff_feed_width.length > 1 && cff_feed_width !== '100%') $cff_width_options.show(); |
| 221 | |
| 222 | jQuery('#cff_feed_width').change(function(){ |
| 223 | cff_feed_width = jQuery(this).val(); |
| 224 | |
| 225 | if( cff_feed_width.length < 2 || cff_feed_width == '100%' ) { |
| 226 | $cff_width_options.slideUp(); |
| 227 | } else { |
| 228 | $cff_width_options.slideDown(); |
| 229 | } |
| 230 | }); |
| 231 | } |
| 232 | |
| 233 | //Scroll to hash for quick links |
| 234 | jQuery('#cff-admin a').click(function() { |
| 235 | if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) { |
| 236 | var target = jQuery(this.hash); |
| 237 | target = target.length ? target : this.hash.slice(1); |
| 238 | if (target.length) { |
| 239 | jQuery('html,body').animate({ |
| 240 | scrollTop: target.offset().top |
| 241 | }, 500); |
| 242 | return false; |
| 243 | } |
| 244 | } |
| 245 | }); |
| 246 | |
| 247 | //Shortcode tooltips |
| 248 | jQuery('#cff-admin label').click(function(){ |
| 249 | var $el = jQuery(this); |
| 250 | var $cff_shortcode = $el.siblings('.cff_shortcode'); |
| 251 | if($cff_shortcode.is(':visible')){ |
| 252 | $el.siblings('.cff_shortcode').css('display','none'); |
| 253 | } else { |
| 254 | $el.siblings('.cff_shortcode').css('display','block'); |
| 255 | } |
| 256 | }); |
| 257 | jQuery('#cff-admin th').hover(function(){ |
| 258 | if( jQuery(this).find('.cff_shortcode').length > 0 ){ |
| 259 | jQuery(this).find('label').append('<code class="cff_shortcode_symbol">[]</code>'); |
| 260 | } |
| 261 | }, function(){ |
| 262 | jQuery(this).find('.cff_shortcode_symbol').remove(); |
| 263 | }); |
| 264 | jQuery('#cff-admin label').hover(function(){ |
| 265 | if( jQuery(this).siblings('.cff_shortcode').length > 0 ){ |
| 266 | jQuery(this).attr('title', 'Click for shortcode option'); |
| 267 | } |
| 268 | }, function(){}); |
| 269 | |
| 270 | //Open/close the expandable option sections |
| 271 | jQuery('.cff-expandable-options').hide(); |
| 272 | jQuery('.cff-expand-button a').on('click', function(e){ |
| 273 | e.preventDefault(); |
| 274 | var $self = jQuery(this); |
| 275 | $self.parent().next('.cff-expandable-options').toggle(); |
| 276 | if( $self.text().indexOf('Show') !== -1 ){ |
| 277 | $self.text( $self.text().replace('Show', 'Hide') ); |
| 278 | } else { |
| 279 | $self.text( $self.text().replace('Hide', 'Show') ); |
| 280 | } |
| 281 | }); |
| 282 | |
| 283 | function cffToggleNummobile() { |
| 284 | if (jQuery('#cff_show_num_mobile').is(':checked')) { |
| 285 | jQuery('#cff_show_num_mobile').closest('td').find('.cff-mobile-col-settings').slideDown(); |
| 286 | } else { |
| 287 | jQuery('#cff_show_num_mobile').closest('td').find('.cff-mobile-col-settings').slideUp(function(){ |
| 288 | jQuery('#cff_num_mobile').val(''); |
| 289 | }); |
| 290 | } |
| 291 | } |
| 292 | cffToggleNummobile(); |
| 293 | jQuery('#cff_show_num_mobile').change(cffToggleNummobile); |
| 294 | |
| 295 | //Facebook login |
| 296 | $('#cff_fb_login').on('click', function(){ |
| 297 | $('#cff_fb_login_modal').show(); |
| 298 | }); |
| 299 | $('#cff_admin_cancel_btn').on('click', function(){ |
| 300 | $('#cff_fb_login_modal').hide(); |
| 301 | }); |
| 302 | $('.cff-modal-close, #cff-close-modal-primary-button').on('click', function(){ |
| 303 | $('.cff_modal_tokens').hide(); |
| 304 | }); |
| 305 | $('#cff_fb_show_tokens').on('click', function(){ |
| 306 | $('.cff_modal_tokens, .cff-groups-list').show(); |
| 307 | $('#cff-group-installation').hide(); |
| 308 | }); |
| 309 | |
| 310 | //Select a page for token |
| 311 | $('.cff-managed-page').on('click', function(){ |
| 312 | $('#cff-insert-token, .cff-insert-reviews-token, .cff-insert-both-tokens').removeAttr('disabled'); |
| 313 | |
| 314 | $('#cff_token_expiration_note').show(); |
| 315 | |
| 316 | var $self = $(this); |
| 317 | if( $self.hasClass('cff-page-selected') ){ |
| 318 | $self.removeClass('cff-page-selected'); |
| 319 | } else { |
| 320 | $self.addClass('cff-page-selected'); |
| 321 | } |
| 322 | }); |
| 323 | |
| 324 | |
| 325 | |
| 326 | |
| 327 | |
| 328 | //Connect Accounts array object |
| 329 | var cff_connected_accounts = {}, |
| 330 | cff_multifeed_enabled = false, |
| 331 | cff_remove_primary_text = 'Remove as Primary Feed', |
| 332 | cff_add_primary_text = 'Make Primary Feed'; |
| 333 | |
| 334 | if( $('#cff_page_id').hasClass('cff_multifeed_enabled') ) cff_multifeed_enabled = true; |
| 335 | if( cff_multifeed_enabled ){ |
| 336 | cff_remove_primary_text = 'Remove from Primary Feed'; |
| 337 | cff_add_primary_text = 'Add to Primary Feed'; |
| 338 | } |
| 339 | |
| 340 | //If there are accounts displayed then assign them to the connected accounts array |
| 341 | var cff_connected_accounts_val = $('#cff_connected_accounts').val(); |
| 342 | if( cff_connected_accounts_val !== '' && cff_connected_accounts_val !== '{}' && typeof cff_connected_accounts_val !== 'undefined' ){ |
| 343 | |
| 344 | cff_connected_accounts = cff_connected_accounts_val.replace(/\\"/g, '"'); |
| 345 | cff_connected_accounts = JSON.parse(cff_connected_accounts); |
| 346 | |
| 347 | createAccountHTML(cff_connected_accounts); |
| 348 | } |
| 349 | |
| 350 | //Insert Page Access Token |
| 351 | $('#cff-insert-token, #cff-insert-all-tokens').on('click', function(){ |
| 352 | |
| 353 | if( $(this).hasClass('cff_connect_all') ) $('.cff-managed-page').addClass('cff-page-selected'); |
| 354 | |
| 355 | var $selectedPage = $('.cff-page-selected'), |
| 356 | selectedPageId = $selectedPage.attr('data-page-id'), |
| 357 | selectedPageToken = $selectedPage.attr('data-token'); |
| 358 | |
| 359 | //Add ID to setting |
| 360 | if( $('#cff_page_id').val().trim() == '' ){ |
| 361 | $('#cff_page_id').val( selectedPageId ).addClass('cff-success'); |
| 362 | cffAddCurIdLabel($('.cff-page-selected').first().find('.cff-page-info-name').text(), $('.cff-page-selected').first().find('.cff-page-avatar').attr('src')); |
| 363 | } |
| 364 | |
| 365 | //Add token to setting |
| 366 | if( $('#cff_access_token').val().trim() == '' || $('#cff_access_token').hasClass('cff-replace-token') ){ |
| 367 | //If multifeed then add ID to front so it's assigned to that ID in the feed |
| 368 | if( $('#cff_page_id').hasClass('cff_multifeed_enabled') ) selectedPageToken = selectedPageId + ':' + selectedPageToken; |
| 369 | |
| 370 | $('#cff_access_token').val( selectedPageToken ).addClass('cff-success'); |
| 371 | } |
| 372 | |
| 373 | if( $(this).hasClass('cff-group-btn') ){ |
| 374 | $('.cff-groups-list').hide(); |
| 375 | $('#cff-group-installation').show(); |
| 376 | |
| 377 | //Show directions for either group admin or member |
| 378 | if( $('.cff-page-selected').hasClass('cff-group-admin') ){ |
| 379 | $('#cff-group-admin-directions').show(); |
| 380 | $('#cff-group-member-directions').hide(); |
| 381 | } else { |
| 382 | $('#cff-group-admin-directions').hide(); |
| 383 | $('#cff-group-member-directions').show(); |
| 384 | } |
| 385 | |
| 386 | //Change page type to be group |
| 387 | $('#cff_page_type').val('group'); |
| 388 | $('.cff-page-options').hide(); |
| 389 | |
| 390 | //Dynamically create group edit link |
| 391 | var cffGroupEditLink = 'https://facebook.com/groups/'+selectedPageId+'/edit'; |
| 392 | $('#cff-group-installation #cff-group-edit').attr('href', cffGroupEditLink); |
| 393 | } else { |
| 394 | $('.cff_modal_tokens').hide(); |
| 395 | } |
| 396 | |
| 397 | // cff_connected_accounts |
| 398 | $('.cff-managed-pages').find('.cff-page-selected').each(function(){ |
| 399 | var $page = $(this); |
| 400 | |
| 401 | addConnectedAccounts( |
| 402 | $page.attr('data-page-id'), |
| 403 | $page.find('.cff-page-info-name').text(), |
| 404 | $page.attr('data-pagetype'), |
| 405 | $page.attr('data-token'), |
| 406 | $page.find('.cff-page-avatar').attr('src') |
| 407 | ); |
| 408 | |
| 409 | }); |
| 410 | |
| 411 | location.hash = "cffnomodal"; |
| 412 | }); |
| 413 | |
| 414 | //Manually connect account |
| 415 | //Step 1 |
| 416 | $('#cff_manual_account_button, #cff-admin .cff_manual_back').on('click', function(e){ |
| 417 | e.preventDefault(); |
| 418 | if( !$(this).hasClass('cff_manual_back') ) $('#cff_manual_account').toggle(); |
| 419 | $('#cff_manual_account_step_1').show(); |
| 420 | $('#cff_manual_account_step_2').hide(); |
| 421 | }); |
| 422 | //Step 2 |
| 423 | jQuery("#cff_manual_account_type").change(function() { |
| 424 | cff_go_to_step_2(); |
| 425 | }); |
| 426 | $('#cff-admin .cff_manual_forward').on('click', function(){ |
| 427 | if( $("#cff_manual_account_type option:selected").val() ){ |
| 428 | cff_go_to_step_2(); |
| 429 | } else { |
| 430 | $("#cff_manual_account_type").addClass('cff_error'); |
| 431 | setTimeout(function(){ $("#cff_manual_account_type").removeClass('cff_error'); }, 500); |
| 432 | } |
| 433 | }); |
| 434 | function cff_go_to_step_2(){ |
| 435 | $('#cff_manual_account_step_2').attr('class', 'cff_account_type_'+jQuery("#cff_manual_account_type option:selected").val() ); |
| 436 | |
| 437 | $('#cff_manual_account_step_1').hide(); |
| 438 | $('#cff_manual_account_step_2').show(); |
| 439 | } |
| 440 | |
| 441 | //Add account |
| 442 | $('#cff_manual_account_step_2 input[type=submit]').on('click', function(e){ |
| 443 | e.preventDefault(); |
| 444 | |
| 445 | var $cff_manual_account = $('#cff_manual_account'); |
| 446 | |
| 447 | addConnectedAccounts( |
| 448 | $cff_manual_account.find('#cff_manual_account_id').val(), |
| 449 | $cff_manual_account.find('#cff_manual_account_name').val(), |
| 450 | $cff_manual_account.find('#cff_manual_account_type').val(), |
| 451 | $cff_manual_account.find('#cff_manual_account_token').val(), |
| 452 | false |
| 453 | ); |
| 454 | }); |
| 455 | |
| 456 | //Only enable manual account submit button if ID/token fields have values |
| 457 | $('#cff_manual_account_id, #cff_manual_account_token').on('input', function() { |
| 458 | if( $('#cff_manual_account_id').val() == '' || $('#cff_manual_account_token').val() == '' ){ |
| 459 | $('#cff_manual_account_step_2 #submit').attr('disabled', true); |
| 460 | } else { |
| 461 | $('#cff_manual_account_step_2 #submit').removeAttr('disabled'); |
| 462 | } |
| 463 | }); |
| 464 | |
| 465 | //Show raw account data (can be used for exporting/importing accounts in bulk) |
| 466 | $('#cff_export_accounts').on('click', function(e){ |
| 467 | e.preventDefault(); |
| 468 | $('#cff_export_accounts_wrap').toggle(); |
| 469 | }); |
| 470 | |
| 471 | |
| 472 | function addConnectedAccounts(id, name, pagetype, accesstoken, avatar=false){ |
| 473 | |
| 474 | if( pagetype == 'page' ) avatar = ''; |
| 475 | |
| 476 | id = cffStripURLParts(id); |
| 477 | |
| 478 | //Add to connected accounts array |
| 479 | cff_connected_accounts[id] = { |
| 480 | id: id, |
| 481 | name: encodeURI( name ), |
| 482 | pagetype: pagetype, |
| 483 | accesstoken: accesstoken, |
| 484 | avatar: avatar |
| 485 | }; |
| 486 | |
| 487 | //Update setting on page |
| 488 | $('#cff_connected_accounts').val( JSON.stringify(cff_connected_accounts) ); |
| 489 | |
| 490 | //Add HTML to page |
| 491 | createAccountHTML(cff_connected_accounts); |
| 492 | } |
| 493 | |
| 494 | function removeConnectedAccount($account){ |
| 495 | //Remove account from array |
| 496 | delete cff_connected_accounts[$account.attr('data-page-id')]; |
| 497 | |
| 498 | //Update setting on page |
| 499 | $('#cff_connected_accounts').val( JSON.stringify(cff_connected_accounts) ); |
| 500 | |
| 501 | //Remove it from primary feed if it's in there |
| 502 | removePrimaryAcount($account); |
| 503 | |
| 504 | //Remove account element from page |
| 505 | $account.remove(); |
| 506 | } |
| 507 | |
| 508 | function cffStripURLParts(string){ |
| 509 | if (typeof string === 'undefined') { |
| 510 | return ''; |
| 511 | } |
| 512 | //If user pastes their full URL into the Page ID field then strip it out |
| 513 | var cff_facebook_string = 'facebook.com', |
| 514 | hasURL = (string.indexOf(cff_facebook_string) > -1); |
| 515 | if (hasURL) { |
| 516 | var stringArr = string.split('?')[0].replace(/\/$/, '').split('/'); |
| 517 | string = stringArr[stringArr.length-1].replace(/[\.\/]/,''); |
| 518 | } |
| 519 | |
| 520 | return string; |
| 521 | } |
| 522 | |
| 523 | function createAccountHTML(cff_connected_accounts){ |
| 524 | |
| 525 | var accountsHTML = ''; |
| 526 | |
| 527 | //Loop through accounts and create HTML |
| 528 | for (var key in cff_connected_accounts) { |
| 529 | if (cff_connected_accounts.hasOwnProperty(key)) { |
| 530 | |
| 531 | var id = cffStripURLParts(cff_connected_accounts[key]['id']), |
| 532 | name = decodeURI(cff_connected_accounts[key]['name']), |
| 533 | pagetype = cff_connected_accounts[key]['pagetype'], |
| 534 | accesstoken = cff_connected_accounts[key]['accesstoken'], |
| 535 | avatar = cff_connected_accounts[key]['avatar'], |
| 536 | cff_account_active = '', |
| 537 | no_avatar = false; |
| 538 | |
| 539 | if( (!avatar || avatar == 'false' ) && pagetype == 'group' ) no_avatar = true; |
| 540 | if( !avatar || avatar == '' ) avatar = 'https://graph.facebook.com/'+id+'/picture'; |
| 541 | |
| 542 | //If it's in use then mark it as primary/active |
| 543 | if( $('#cff_page_id').val().indexOf(id) !== -1 ) cff_account_active = ' cff_account_active'; |
| 544 | |
| 545 | accountsHTML += '<div class="cff_connected_account cff_account_type_'+pagetype+cff_account_active+'" id="cff_connected_account_'+id+'" data-accesstoken="'+accesstoken+'" data-pagetype="'+pagetype+'" data-page-id="'+id+'">' + |
| 546 | '<div class="cff_ca_info">' + |
| 547 | '<div class="cff_ca_delete"><a href="JavaScript:void(0);" class="cff_delete_account"><i class="fa fa-times"></i><span class="cff_remove_text">Remove</span></a></div>'+ |
| 548 | '<div class="cff_ca_username">'; |
| 549 | ( no_avatar ) ? accountsHTML += '' : accountsHTML += '<img class="cff_ca_avatar" src="'+avatar+'">'; |
| 550 | accountsHTML += '<strong><span class="cff_ca_fullname">'+name+'</span><span class="cff_ca_pagetype">'+pagetype+' ID: '+id+'</span></strong>' + |
| 551 | '</div>' + |
| 552 | '<div class="cff_ca_actions">' + |
| 553 | '<a href="JavaScript:void(0);" class="cff_make_primary">'; |
| 554 | if( cff_account_active !== '' ){ |
| 555 | accountsHTML += '<i class="fa fa-minus-circle" aria-hidden="true"></i>'+cff_remove_primary_text; |
| 556 | } else { |
| 557 | accountsHTML += '<i class="fa fa-plus-circle" aria-hidden="true"></i>'+cff_add_primary_text; |
| 558 | } |
| 559 | accountsHTML += '</a>'; |
| 560 | |
| 561 | if( $('#cff_page_access_token').length && pagetype == 'page' ) accountsHTML += '<a href="JavaScript:void(0);" class="cff_make_reviews"><i class="fa fa-star" aria-hidden="true"></i>Use for Reviews Feed</a>'; |
| 562 | |
| 563 | accountsHTML += '<a class="cff_ca_token_shortcode" href="JavaScript:void(0);"><i class="fa fa-chevron-circle-right" aria-hidden="true"></i>Add to another Feed</a>' + |
| 564 | '<p class="cff_ca_show_token"><a href="javascript:void(0);" id="cff_ca_show_token_'+id+'"><i class="fa fa-ellipsis-h" style="margin: 0; font-size: 12px;" aria-hidden="true"></i></a></p>' + |
| 565 | '</div>' + |
| 566 | '<div class="cff_ca_shortcode">' + |
| 567 | '<p>Copy and paste this shortcode into your page or widget area:<br>' + |
| 568 | '<code>[custom-facebook-feed account="'+id+'" pagetype="'+pagetype+'"]</code>' + |
| 569 | '</p>'; |
| 570 | if( cff_multifeed_enabled ) accountsHTML += '<p>To add multiple accounts in the same feed, simply separate them using commas:<br>' + |
| 571 | '<code>[custom-facebook-feed account="'+id+', account_2, account_3"]</code>' + |
| 572 | '</p>'; |
| 573 | accountsHTML += '<p>Click <a href="https://smashballoon.com/custom-facebook-feed/docs/shortcodes/" target="_blank">here</a> to learn more about shortcodes</p>' + |
| 574 | '</div>' + |
| 575 | '<div class="cff_ca_accesstoken">' + |
| 576 | '<span class="cff_ca_token_label">Access Token:</span><input type="text" class="cff_ca_token" value="'+accesstoken+'" readonly="readonly" onclick="this.focus();this.select()" title="To copy, click the field then press Ctrl + C (PC) or Cmd + C (Mac).">' + |
| 577 | '</div>' + |
| 578 | '</div>' + |
| 579 | '</div>'; |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | //Add HTML to page |
| 584 | $('#cff_connected_accounts_wrap').html(accountsHTML); |
| 585 | |
| 586 | //Add Raw Data button |
| 587 | $('.cff_connected_actions').show(); |
| 588 | |
| 589 | } |
| 590 | |
| 591 | function removePrimaryAcount($account){ |
| 592 | //Remove ID/token from fields |
| 593 | if( $account.hasClass('cff_account_active') ){ |
| 594 | |
| 595 | var selected_id = $account.attr('data-page-id'), |
| 596 | selected_token = $account.attr('data-accesstoken'); |
| 597 | |
| 598 | //Remove as primary account |
| 599 | cffLabelAsPrimary($account); |
| 600 | |
| 601 | $('#cff_primary_account_label').hide(); |
| 602 | |
| 603 | if( cff_multifeed_enabled ){ |
| 604 | |
| 605 | //Find the ID from the removed account and remove it from the ID field |
| 606 | var updatedIdVal = $('#cff_page_id').val().replace(selected_id, ''); |
| 607 | //Remove any stray commas left over |
| 608 | updatedIdVal = updatedIdVal.replace(',,', '').replace(' ,', '').replace(/^, |, $/g,''); |
| 609 | |
| 610 | $('#cff_page_id').val( updatedIdVal ).removeClass('cff-success'); |
| 611 | |
| 612 | //Remove Token |
| 613 | // var updatedTokenVal = $('#cff_access_token').val().replace(selected_id+':'+selected_token, '').replace(selected_token, ''); |
| 614 | var updatedTokenVal = $('#cff_access_token').val().replace(selected_id+':'+selected_token, ''); |
| 615 | //Remove any stray commas left over |
| 616 | updatedTokenVal = updatedTokenVal.replace(',,', '').replace(' ,', '').replace(':,', ':').replace(/^, |, $/g,''); |
| 617 | |
| 618 | $('#cff_access_token').val( updatedTokenVal ).removeClass('cff-success'); |
| 619 | |
| 620 | |
| 621 | } else { |
| 622 | |
| 623 | //Revert ID/token fields back to previous values |
| 624 | $('#cff_page_id').val( $('#cff_page_id').attr('data-page-id') ).removeClass('cff-success'); |
| 625 | $('#cff_access_token').val( $('#cff_access_token').attr('data-accesstoken') ).removeClass('cff-success'); |
| 626 | |
| 627 | } |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | |
| 632 | var $body = $('body'); |
| 633 | //Show Access Token |
| 634 | $body.on('click', '.cff_ca_show_token a', function(e) { |
| 635 | e.preventDefault(); |
| 636 | jQuery(this).closest('.cff_ca_info').find('.cff_ca_accesstoken').slideToggle(200); |
| 637 | }); |
| 638 | $body.on('click', '.cff_ca_token_shortcode, .cff_make_primary, .cff_make_reviews', function (event) { |
| 639 | event.preventDefault(); |
| 640 | var $clicked = $(event.target); |
| 641 | //Show shortcode |
| 642 | if( $clicked.hasClass('cff_ca_token_shortcode') ) { |
| 643 | jQuery(this).closest('.cff_ca_info').find('.cff_ca_shortcode').slideToggle(200); |
| 644 | } |
| 645 | //Make Reviews account |
| 646 | if( $clicked.hasClass('cff_make_reviews') ){ |
| 647 | $('#cff_page_access_token').val( $clicked.closest('.cff_connected_account').attr('data-accesstoken') ).addClass('cff-success'); |
| 648 | } |
| 649 | //Make primary account |
| 650 | if( $clicked.hasClass('cff_make_primary') ){ |
| 651 | var $selected_account = $clicked.closest('.cff_connected_account'), |
| 652 | selected_id = $selected_account.attr('data-page-id'), |
| 653 | selected_token = $selected_account.attr('data-accesstoken'); |
| 654 | |
| 655 | |
| 656 | //Remove ID/token from fields |
| 657 | if( $selected_account.hasClass('cff_account_active') ){ |
| 658 | |
| 659 | removePrimaryAcount($selected_account); |
| 660 | |
| 661 | //Add ID/token to fields |
| 662 | } else { |
| 663 | |
| 664 | //Add as primary account |
| 665 | cffLabelAsPrimary($selected_account, true); |
| 666 | |
| 667 | //Add ID/token to fields |
| 668 | if( cff_multifeed_enabled ){ |
| 669 | |
| 670 | //Add ID to existing IDs already in field |
| 671 | var id_sep = ', ', |
| 672 | existing_id = $('#cff_page_id').val().trim(), |
| 673 | existing_token = $('#cff_access_token').val().trim(); |
| 674 | |
| 675 | if( existing_id == '' ) id_sep = ''; |
| 676 | $('#cff_page_id').val( existing_id + id_sep + selected_id ).addClass('cff-success'); |
| 677 | |
| 678 | //Change to multiple token format |
| 679 | var token_format = ''; |
| 680 | if( existing_token !== '' ) token_format += existing_token + ', '; |
| 681 | token_format += selected_id + ':' + selected_token; |
| 682 | |
| 683 | $('#cff_access_token').val( token_format ).addClass('cff-success'); |
| 684 | |
| 685 | } else { |
| 686 | |
| 687 | //Replace existing ID and token |
| 688 | $('#cff_page_id').val( selected_id ).addClass('cff-success'); |
| 689 | $('#cff_access_token').val( selected_token ).addClass('cff-success'); |
| 690 | |
| 691 | //Remove active account class from other accounts |
| 692 | $selected_account.siblings().each(function(){ |
| 693 | cffLabelAsPrimary($(this)); |
| 694 | }); |
| 695 | |
| 696 | } |
| 697 | |
| 698 | |
| 699 | } |
| 700 | } |
| 701 | }); |
| 702 | //Remove account |
| 703 | $body.on('click', '.cff_delete_account', function(){ |
| 704 | removeConnectedAccount( $(this).closest('.cff_connected_account') ); |
| 705 | }); |
| 706 | |
| 707 | //Change button label when adding/removing as primary account |
| 708 | function cffLabelAsPrimary($account, makePrimary=false){ |
| 709 | if( makePrimary ){ |
| 710 | $account.addClass('cff_account_active').find('.cff_make_primary').html('<i class="fa fa-minus-circle" aria-hidden="true"></i>'+cff_remove_primary_text); |
| 711 | |
| 712 | if( $account.length > 0 ) cffAddCurIdLabel($account.find('.cff_ca_fullname').text(), $account.find('.cff_ca_avatar').attr('src')); |
| 713 | |
| 714 | } else { |
| 715 | $account.removeClass('cff_account_active').find('.cff_make_primary').html('<i class="fa fa-plus-circle" aria-hidden="true"></i>'+cff_add_primary_text); |
| 716 | } |
| 717 | } |
| 718 | |
| 719 | function cffAddCurIdLabel(name, avatar){ |
| 720 | var account_img = '', |
| 721 | account_name = '<span>' + name + '</span>'; |
| 722 | if( avatar !== undefined ) account_img = '<img src="' + avatar + '" />'; |
| 723 | |
| 724 | $('#cff_primary_account_label').show().html( account_img + account_name ); |
| 725 | } |
| 726 | |
| 727 | //Label a primary account when page is first loaded |
| 728 | var cff_current_page_id = $('#cff_page_id').val(), |
| 729 | cff_current_page_id_arr = []; |
| 730 | if( typeof cff_current_page_id !== 'undefined' ) var cff_current_page_id_arr = cff_current_page_id.split(','); |
| 731 | |
| 732 | if( cff_current_page_id_arr.length > 1 ){ |
| 733 | for (var i = 0; i < cff_current_page_id_arr.length; i++) { |
| 734 | cffLabelAsPrimary( $('#cff_connected_account_' + cffValidateID( cff_current_page_id_arr[i] ) ), true ); |
| 735 | } |
| 736 | } else { |
| 737 | cffLabelAsPrimary( $('#cff_connected_account_' + cffValidateID( cff_current_page_id ) ), true ); |
| 738 | } |
| 739 | //Make sure ID is a valid string |
| 740 | function cffValidateID(id){ |
| 741 | if( typeof id === 'undefined' || id == '' ) return; |
| 742 | |
| 743 | //Remove slashes from end |
| 744 | id = cffStripURLParts( id.replace(/\/$/, "").trim() ); |
| 745 | //Only return if it contains numbers/letters |
| 746 | if( id.match("^[A-Za-z0-9]+$") ) return id; |
| 747 | } |
| 748 | |
| 749 | //Show the modal by default, but hide if the "cffnomodal" class is added to prevent it showing after saving settings |
| 750 | if( location.hash !== '#cffnomodal' ){ |
| 751 | $('.cff_modal_tokens').removeClass('cffnomodal'); |
| 752 | } |
| 753 | |
| 754 | //Switch Page/Group app button in modal |
| 755 | jQuery("#cff_login_type").change(function() { |
| 756 | if ( jQuery("#cff_login_type option:selected").val() == 'group' ) { |
| 757 | jQuery('#cff_page_app').hide(); |
| 758 | jQuery('#cff_group_app').css('display', 'inline-block'); |
| 759 | } else { |
| 760 | jQuery('#cff_page_app').css('display', 'inline-block'); |
| 761 | jQuery('#cff_group_app').hide(); |
| 762 | } |
| 763 | }); |
| 764 | |
| 765 | //Load the admin share widgets |
| 766 | $('#cff-admin-show-share-links').on('click', function(){ |
| 767 | $(this).fadeOut(); |
| 768 | if( $('#cff-admin-share-links iframe').length == 0 ) $('#cff-admin-share-links').html('<a href="https://twitter.com/share" class="twitter-share-button" data-url="https://wordpress.org/plugins/custom-facebook-feed/" data-text="Display your Facebook posts on your site your way using the Custom Facebook Feed WordPress plugin!" data-via="smashballoon" data-dnt="true">Tweet</a> <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?"http":"https";if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+"://platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document, "script", "twitter-wjs");</script> <style type="text/css"> #twitter-widget-0{float: left; width: 82px !important;}.IN-widget{margin-right: 20px;}</style> <div id="fb-root" style="display: none;"></div><script>(function(d, s, id){var js, fjs=d.getElementsByTagName(s)[0]; if (d.getElementById(id)) return; js=d.createElement(s); js.id=id; js.src="//connect.facebook.net/en_GB/sdk.js#xfbml=1&version=v2.0"; fjs.parentNode.insertBefore(js, fjs);}(document, "script", "facebook-jssdk"));</script> <div class="fb-like" data-href="https://wordpress.org/plugins/custom-facebook-feed/" data-layout="button_count" data-action="like" data-show-faces="false" data-share="true" style="display: block; float: left; margin-right: 5px;"></div><script src="//platform.linkedin.com/in.js" type="text/javascript"> lang: en_US </script> <script type="IN/Share" data-url="https://wordpress.org/plugins/custom-facebook-feed/"></script></div>'); |
| 769 | |
| 770 | setTimeout(function(){ |
| 771 | $('#cff-admin-share-links').addClass('cff-show'); |
| 772 | }, 500); |
| 773 | }); |
| 774 | |
| 775 | //Group app setup screenshot |
| 776 | jQuery('#cff-group-app-tooltip').hover(function(){ |
| 777 | jQuery('#cff-group-app-screenshot').fadeIn(100); |
| 778 | }, function(){ |
| 779 | jQuery('#cff-group-app-screenshot').fadeOut(100); |
| 780 | }); |
| 781 | |
| 782 | //Remove any duplicate groups |
| 783 | jQuery('.cff-group-admin').each(function(){ |
| 784 | jQuery('.cff-groups-list #' + jQuery(this).attr('id') ).eq(1).hide(); |
| 785 | }); |
| 786 | |
| 787 | |
| 788 | //Show/hide mobile column setting |
| 789 | var cff_masonry_desktop_col = jQuery('#cff_cols').val(), |
| 790 | $cff_mobile_col_settings = jQuery('.cff-mobile-col-settings'); |
| 791 | if( typeof cff_post_limit === 'undefined' ) cff_masonry_desktop_col = '1'; |
| 792 | |
| 793 | //Should we show anything initially? |
| 794 | if( cff_masonry_desktop_col == '1' ) $cff_mobile_col_settings.hide(); |
| 795 | if( parseInt(cff_masonry_desktop_col) > 1 ){ |
| 796 | $cff_mobile_col_settings.show(); |
| 797 | } |
| 798 | |
| 799 | jQuery('#cff_cols').change(function(){ |
| 800 | cff_cols_num = parseInt( jQuery('#cff_cols').val() ); |
| 801 | |
| 802 | if(cff_cols_num > 1){ |
| 803 | $cff_mobile_col_settings.slideDown(200); |
| 804 | } else { |
| 805 | $cff_mobile_col_settings.slideUp(200); |
| 806 | } |
| 807 | }); |
| 808 | |
| 809 | // notices |
| 810 | |
| 811 | if (jQuery('#cff-notice-bar').length) { |
| 812 | jQuery('#wpadminbar').after(jQuery('#cff-notice-bar')); |
| 813 | jQuery('#wpcontent').css('padding-left', 0); |
| 814 | jQuery('#wpbody').css('padding-left', '20px'); |
| 815 | jQuery('#cff-notice-bar').show(); |
| 816 | } |
| 817 | |
| 818 | jQuery('#cff-notice-bar .dismiss').click(function(e) { |
| 819 | e.preventDefault(); |
| 820 | jQuery('#cff-notice-bar').remove(); |
| 821 | jQuery.ajax({ |
| 822 | url: cffA.ajax_url, |
| 823 | type: 'post', |
| 824 | data: { |
| 825 | action : 'cff_lite_dismiss', |
| 826 | cff_nonce: cffA.cff_nonce |
| 827 | }, |
| 828 | success: function (data) { |
| 829 | } |
| 830 | }); |
| 831 | }); |
| 832 | |
| 833 | jQuery('#cff-oembed-disable').click(function(e) { |
| 834 | e.preventDefault(); |
| 835 | jQuery(this).addClass( 'loading' ).html('<i class="fa fa-spinner fa-spin" aria-hidden="true"></i>'); |
| 836 | jQuery.ajax({ |
| 837 | url: cffA.ajax_url, |
| 838 | type: 'post', |
| 839 | data: { |
| 840 | action : 'cff_oembed_disable', |
| 841 | cff_nonce: cffA.cff_nonce |
| 842 | }, |
| 843 | success: function (data) { |
| 844 | jQuery('#cff-oembed-disable').closest('p').html(data); |
| 845 | } |
| 846 | }); |
| 847 | }); |
| 848 | |
| 849 | |
| 850 | |
| 851 | //sb_instagram_enable_email_report |
| 852 | function cffToggleEmail() { |
| 853 | if (jQuery('#cff_enable_email_report').is(':checked')) { |
| 854 | jQuery('#cff_enable_email_report').closest('td').find('.cff_box').slideDown(); |
| 855 | } else { |
| 856 | jQuery('#cff_enable_email_report').closest('td').find('.cff_box').slideUp(); |
| 857 | } |
| 858 | }cffToggleEmail(); |
| 859 | jQuery('#cff_enable_email_report').change(cffToggleEmail); |
| 860 | if (jQuery('#cff-goto').length) { |
| 861 | jQuery('#cff-goto').closest('tr').addClass('cff-goto'); |
| 862 | $('html, body').animate({ |
| 863 | scrollTop: $('#cff-goto').offset().top - 200 |
| 864 | }, 500); |
| 865 | } |
| 866 | |
| 867 | jQuery('.cff-error-directions .cff-reconnect').click(function(){ |
| 868 | event.preventDefault(); |
| 869 | jQuery('.cff_admin_btn').trigger('click'); |
| 870 | }); |
| 871 | jQuery('.cff-clear-errors-visit-page').click(function(event) { |
| 872 | event.preventDefault(); |
| 873 | var $btn = jQuery(this); |
| 874 | $btn.prop( 'disabled', true ).addClass( 'loading' ).html('<i class="fa fa-spinner fa-spin" aria-hidden="true"></i>'); |
| 875 | $.ajax({ |
| 876 | url : cffA.ajax_url, |
| 877 | type : 'post', |
| 878 | data : { |
| 879 | action : 'cff_reset_log' |
| 880 | }, |
| 881 | success : function(data) { |
| 882 | window.location.href = $btn.attr('data-url'); |
| 883 | }, |
| 884 | error : function(data) { |
| 885 | window.location.href = $btn.attr('data-url'); |
| 886 | } |
| 887 | }); // ajax call |
| 888 | }); |
| 889 | |
| 890 | /* removing padding */ |
| 891 | if (jQuery('#cff-admin-about').length) { |
| 892 | jQuery('#wpcontent').css('padding', 0); |
| 893 | } |
| 894 | |
| 895 | $('.cff-opt-in').click(function(event) { |
| 896 | event.preventDefault(); |
| 897 | |
| 898 | var $btn = jQuery(this); |
| 899 | $btn.prop( 'disabled', true ).addClass( 'loading' ).html('<i class="fa fa-spinner fa-spin" aria-hidden="true"></i>'); |
| 900 | |
| 901 | cffSubmitOptIn(true); |
| 902 | }); // clear_comment_cache click |
| 903 | |
| 904 | $('.cff-no-usage-opt-out').click(function(event) { |
| 905 | event.preventDefault(); |
| 906 | |
| 907 | var $btn = jQuery(this); |
| 908 | $btn.prop( 'disabled', true ).addClass( 'loading' ).html('<i class="fa fa-spinner fa-spin" aria-hidden="true"></i>'); |
| 909 | |
| 910 | cffSubmitOptIn(false); |
| 911 | }); // clear_comment_cache click |
| 912 | |
| 913 | function cffSubmitOptIn(choice) { |
| 914 | $.ajax({ |
| 915 | url : cffA.ajax_url, |
| 916 | type : 'post', |
| 917 | data : { |
| 918 | action : 'cff_usage_opt_in_or_out', |
| 919 | opted_in: choice, |
| 920 | }, |
| 921 | success : function(data) { |
| 922 | $('.cff-no-usage-opt-out').closest('.cff-admin-notice').fadeOut(); |
| 923 | } |
| 924 | }); // ajax call |
| 925 | } |
| 926 | |
| 927 | //Click event for other plugins in menu |
| 928 | $('.cff_get_sbi, .cff_get_cff, .cff_get_ctf, .cff_get_yt').parent().on('click', function(e){ |
| 929 | e.preventDefault(); |
| 930 | |
| 931 | jQuery('.sb_cross_install_modal').remove(); |
| 932 | |
| 933 | $('#wpbody-content').prepend('<div class="sb_cross_install_modal"><div class="sb_cross_install_inner" id="cff-admin-about"><div id="cff-admin-addons"><div class="addons-container"><i class="fa fa-spinner fa-spin cff-loader" aria-hidden="true"></i></div></div></div></div>'); |
| 934 | |
| 935 | var $self = $(this).find('span'), |
| 936 | sb_get_plugin = 'custom_twitter_feeds'; |
| 937 | |
| 938 | if( $self.hasClass('cff_get_cff') ){ |
| 939 | sb_get_plugin = 'custom_facebook_feed'; |
| 940 | } else if( $self.hasClass('cff_get_sbi') ){ |
| 941 | sb_get_plugin = 'instagram_feed'; |
| 942 | } else if( $self.hasClass('cff_get_yt') ){ |
| 943 | sb_get_plugin = 'feeds_for_youtube'; |
| 944 | } |
| 945 | |
| 946 | $get_plugins_url = cffA.ajax_url.replace('admin-ajax.php', ''); |
| 947 | |
| 948 | // Get the quick install box from the about page |
| 949 | $('.sb_cross_install_modal .addons-container').load($get_plugins_url+'admin.php?page=cff-top&tab=more #install_'+sb_get_plugin); |
| 950 | }); |
| 951 | //Close the modal if clicking anywhere outside it |
| 952 | jQuery('body').on('click', '.sb_cross_install_modal', function(e){ |
| 953 | if (e.target !== this) return; |
| 954 | jQuery('.sb_cross_install_modal').remove(); |
| 955 | }); |
| 956 | |
| 957 | }); |
| 958 | |
| 959 | |
| 960 | |
| 961 | /* global smash_admin, jconfirm, wpCookies, Choices, List */ |
| 962 | |
| 963 | (function($) { |
| 964 | |
| 965 | 'use strict'; |
| 966 | |
| 967 | // Global settings access. |
| 968 | var s; |
| 969 | |
| 970 | // Admin object. |
| 971 | var SmashCFFAdmin = { |
| 972 | |
| 973 | // Settings. |
| 974 | settings: { |
| 975 | iconActivate: '<i class="fa fa-toggle-on fa-flip-horizontal" aria-hidden="true"></i>', |
| 976 | iconDeactivate: '<i class="fa fa-toggle-on" aria-hidden="true"></i>', |
| 977 | iconInstall: '<i class="fa fa-cloud-download" aria-hidden="true"></i>', |
| 978 | iconSpinner: '<i class="fa fa-spinner fa-spin" aria-hidden="true"></i>', |
| 979 | mediaFrame: false |
| 980 | }, |
| 981 | |
| 982 | /** |
| 983 | * Start the engine. |
| 984 | * |
| 985 | * @since 1.3.9 |
| 986 | */ |
| 987 | init: function() { |
| 988 | |
| 989 | // Settings shortcut. |
| 990 | s = this.settings; |
| 991 | |
| 992 | // Document ready. |
| 993 | $( document ).ready( SmashCFFAdmin.ready ); |
| 994 | |
| 995 | // Addons List. |
| 996 | SmashCFFAdmin.initAddons(); |
| 997 | }, |
| 998 | |
| 999 | /** |
| 1000 | * Document ready. |
| 1001 | * |
| 1002 | * @since 1.3.9 |
| 1003 | */ |
| 1004 | ready: function() { |
| 1005 | |
| 1006 | // Action available for each binding. |
| 1007 | $( document ).trigger( 'smashReady' ); |
| 1008 | }, |
| 1009 | |
| 1010 | //--------------------------------------------------------------------// |
| 1011 | // Addons List. |
| 1012 | //--------------------------------------------------------------------// |
| 1013 | |
| 1014 | /** |
| 1015 | * Element bindings for Addons List page. |
| 1016 | * |
| 1017 | * @since 1.3.9 |
| 1018 | */ |
| 1019 | initAddons: function() { |
| 1020 | |
| 1021 | // Some actions have to be delayed to document.ready. |
| 1022 | $( document ).on( 'smashReady', function() { |
| 1023 | |
| 1024 | // Only run on the addons page. |
| 1025 | if ( ! $( '#cff-admin-addons' ).length ) { |
| 1026 | return; |
| 1027 | } |
| 1028 | |
| 1029 | // Display all addon boxes as the same height. |
| 1030 | if( $( '#cff-admin-about.cff-admin-wrap').length ){ |
| 1031 | $( '#cff-admin-about .addon-item .details' ).matchHeight( { byrow: false, property: 'height' } ); |
| 1032 | } |
| 1033 | |
| 1034 | // Addons searching. |
| 1035 | if ( $('#cff-admin-addons-list').length ) { |
| 1036 | var addonSearch = new List( 'cff-admin-addons-list', { |
| 1037 | valueNames: [ 'addon-name' ] |
| 1038 | } ); |
| 1039 | |
| 1040 | $( '#cff-admin-addons-search' ).on( 'keyup', function () { |
| 1041 | var searchTerm = $( this ).val(), |
| 1042 | $heading = $( '#addons-heading' ); |
| 1043 | |
| 1044 | if ( searchTerm ) { |
| 1045 | $heading.text( cff_admin.addon_search ); |
| 1046 | } |
| 1047 | else { |
| 1048 | $heading.text( $heading.data( 'text' ) ); |
| 1049 | } |
| 1050 | |
| 1051 | addonSearch.search( searchTerm ); |
| 1052 | } ); |
| 1053 | } |
| 1054 | }); |
| 1055 | |
| 1056 | // Toggle an addon state. |
| 1057 | $( document ).on( 'click', '#cff-admin-addons .addon-item button', function( event ) { |
| 1058 | |
| 1059 | event.preventDefault(); |
| 1060 | |
| 1061 | if ( $( this ).hasClass( 'disabled' ) ) { |
| 1062 | return false; |
| 1063 | } |
| 1064 | |
| 1065 | SmashCFFAdmin.addonToggle( $( this ) ); |
| 1066 | }); |
| 1067 | }, |
| 1068 | |
| 1069 | /** |
| 1070 | * Toggle addon state. |
| 1071 | * |
| 1072 | * @since 1.3.9 |
| 1073 | */ |
| 1074 | addonToggle: function( $btn ) { |
| 1075 | |
| 1076 | var $addon = $btn.closest( '.addon-item' ), |
| 1077 | plugin = $btn.attr( 'data-plugin' ), |
| 1078 | plugin_type = $btn.attr( 'data-type' ), |
| 1079 | action, |
| 1080 | cssClass, |
| 1081 | statusText, |
| 1082 | buttonText, |
| 1083 | errorText, |
| 1084 | successText; |
| 1085 | |
| 1086 | if ( $btn.hasClass( 'status-go-to-url' ) ) { |
| 1087 | // Open url in new tab. |
| 1088 | window.open( $btn.attr('data-plugin'), '_blank' ); |
| 1089 | return; |
| 1090 | } |
| 1091 | |
| 1092 | $btn.prop( 'disabled', true ).addClass( 'loading' ); |
| 1093 | $btn.html( s.iconSpinner ); |
| 1094 | |
| 1095 | if ( $btn.hasClass( 'status-active' ) ) { |
| 1096 | // Deactivate. |
| 1097 | action = 'cff_deactivate_addon'; |
| 1098 | cssClass = 'status-inactive'; |
| 1099 | if ( plugin_type === 'plugin' ) { |
| 1100 | cssClass += ' button button-secondary'; |
| 1101 | } |
| 1102 | statusText = cff_admin.addon_inactive; |
| 1103 | buttonText = cff_admin.addon_activate; |
| 1104 | if ( plugin_type === 'addon' ) { |
| 1105 | buttonText = s.iconActivate + buttonText; |
| 1106 | } |
| 1107 | errorText = s.iconDeactivate + cff_admin.addon_deactivate; |
| 1108 | |
| 1109 | } else if ( $btn.hasClass( 'status-inactive' ) ) { |
| 1110 | // Activate. |
| 1111 | action = 'cff_activate_addon'; |
| 1112 | cssClass = 'status-active'; |
| 1113 | if ( plugin_type === 'plugin' ) { |
| 1114 | cssClass += ' button button-secondary disabled'; |
| 1115 | } |
| 1116 | statusText = cff_admin.addon_active; |
| 1117 | buttonText = cff_admin.addon_deactivate; |
| 1118 | if ( plugin_type === 'addon' ) { |
| 1119 | buttonText = s.iconDeactivate + buttonText; |
| 1120 | } else if ( plugin_type === 'plugin' ) { |
| 1121 | buttonText = cff_admin.addon_activated; |
| 1122 | } |
| 1123 | errorText = s.iconActivate + cff_admin.addon_activate; |
| 1124 | |
| 1125 | } else if ( $btn.hasClass( 'status-download' ) ) { |
| 1126 | // Install & Activate. |
| 1127 | action = 'cff_install_addon'; |
| 1128 | cssClass = 'status-active'; |
| 1129 | if ( plugin_type === 'plugin' ) { |
| 1130 | cssClass += ' button disabled'; |
| 1131 | } |
| 1132 | statusText = cff_admin.addon_active; |
| 1133 | buttonText = cff_admin.addon_activated; |
| 1134 | if ( plugin_type === 'addon' ) { |
| 1135 | buttonText = s.iconActivate + cff_admin.addon_deactivate; |
| 1136 | } |
| 1137 | errorText = s.iconInstall + cff_admin.addon_activate; |
| 1138 | |
| 1139 | } else { |
| 1140 | return; |
| 1141 | } |
| 1142 | |
| 1143 | var data = { |
| 1144 | action: action, |
| 1145 | nonce : cff_admin.nonce, |
| 1146 | plugin: plugin, |
| 1147 | type : plugin_type |
| 1148 | }; |
| 1149 | $.post( cff_admin.ajax_url, data, function( res ) { |
| 1150 | |
| 1151 | if ( res.success ) { |
| 1152 | if ( 'cff_install_addon' === action ) { |
| 1153 | $btn.attr( 'data-plugin', res.data.basename ); |
| 1154 | successText = res.data.msg; |
| 1155 | if ( ! res.data.is_activated ) { |
| 1156 | cssClass = 'status-inactive'; |
| 1157 | if ( plugin_type === 'plugin' ) { |
| 1158 | cssClass = 'button'; |
| 1159 | } |
| 1160 | statusText = cff_admin.addon_inactive; |
| 1161 | buttonText = s.iconActivate + cff_admin.addon_activate; |
| 1162 | } |
| 1163 | } else { |
| 1164 | successText = res.data; |
| 1165 | } |
| 1166 | $addon.find( '.actions' ).append( '<div class="msg success">'+successText+'</div>' ); |
| 1167 | $addon.find( 'span.status-label' ) |
| 1168 | .removeClass( 'status-active status-inactive status-download' ) |
| 1169 | .addClass( cssClass ) |
| 1170 | .removeClass( 'button button-primary button-secondary disabled' ) |
| 1171 | .text( statusText ); |
| 1172 | $btn |
| 1173 | .removeClass( 'status-active status-inactive status-download' ) |
| 1174 | .removeClass( 'button button-primary button-secondary disabled' ) |
| 1175 | .addClass( cssClass ).html( buttonText ); |
| 1176 | } else { |
| 1177 | if ( 'download_failed' === res.data[0].code ) { |
| 1178 | if ( plugin_type === 'addon' ) { |
| 1179 | $addon.find( '.actions' ).append( '<div class="msg error">'+cff_admin.addon_error+'</div>' ); |
| 1180 | } else { |
| 1181 | $addon.find( '.actions' ).append( '<div class="msg error">'+cff_admin.plugin_error+'</div>' ); |
| 1182 | } |
| 1183 | } else { |
| 1184 | $addon.find( '.actions' ).append( '<div class="msg error">'+res.data+'</div>' ); |
| 1185 | } |
| 1186 | $btn.html( errorText ); |
| 1187 | } |
| 1188 | |
| 1189 | $btn.prop( 'disabled', false ).removeClass( 'loading' ); |
| 1190 | |
| 1191 | // Automatically clear addon messages after 3 seconds. |
| 1192 | setTimeout( function() { |
| 1193 | $( '.addon-item .msg' ).remove(); |
| 1194 | }, 3000 ); |
| 1195 | |
| 1196 | }).fail( function( xhr ) { |
| 1197 | console.log( xhr.responseText ); |
| 1198 | }); |
| 1199 | }, |
| 1200 | |
| 1201 | }; |
| 1202 | |
| 1203 | SmashCFFAdmin.init(); |
| 1204 | |
| 1205 | window.SmashCFFAdmin = SmashCFFAdmin; |
| 1206 | |
| 1207 | })( jQuery ); |