listing.js
52 lines
| 1 | import jQuery from 'jquery'; |
| 2 | import apiFetch from '@wordpress/api-fetch'; |
| 3 | |
| 4 | /** |
| 5 | * Get disabled ads status |
| 6 | * |
| 7 | * @param {number} id |
| 8 | */ |
| 9 | function getPostData( id ) { |
| 10 | apiFetch( { |
| 11 | path: `/advanced-ads/v1/page_quick_edit?id=${ id }&nonce=${ window.advancedAds.page_quick_edit.nonce }`, |
| 12 | method: 'GET', |
| 13 | } ).then( function ( data ) { |
| 14 | setQuickEditValues( id, data ); |
| 15 | } ); |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * Update checkboxes states upon click on a quick edit link |
| 20 | * |
| 21 | * @param {number} id |
| 22 | * @param {Object} data |
| 23 | */ |
| 24 | function setQuickEditValues( id, data ) { |
| 25 | const theRow = jQuery( `#edit-${ id }` ); |
| 26 | const disableAds = theRow.find( '[name="advads-disable-ads"]' ); |
| 27 | disableAds.closest( 'fieldset' ).prop( 'disabled', false ); |
| 28 | disableAds.prop( 'checked', Boolean( data.disable_ads ) ); |
| 29 | const inContent = theRow.find( '[name="advads-disable-the-content"]' ); |
| 30 | if ( inContent.length ) { |
| 31 | inContent |
| 32 | .prop( 'disabled', false ) |
| 33 | .prop( 'checked', Boolean( data.disable_the_content ) ); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | jQuery( function () { |
| 38 | const editCopy = window.inlineEditPost.edit; |
| 39 | |
| 40 | // Replace the default WP function |
| 41 | window.inlineEditPost.edit = function ( id ) { |
| 42 | /* eslint-enable no-undef */ |
| 43 | // Call the original WP edit function. |
| 44 | editCopy.apply( this, arguments ); |
| 45 | |
| 46 | // Now we do our stuff. |
| 47 | if ( 'object' === typeof id ) { |
| 48 | getPostData( parseInt( this.getId( id ) ) ); |
| 49 | } |
| 50 | }; |
| 51 | } ); |
| 52 |